7 Replies Latest reply on Oct 26, 2012 2:41 PM by Marcio Justo

    How to add a4j:support programmatically in richfaces 4

    Chris Mathrusse Newbie

      I'm attempting to build my UI components programmatically due to the dynamic nature of the data being returned from the database. I need to add an onclick event to a component, which in richfaces 3.xx I would simply use a4j:support tag / component, but this is no longer available from looking at the libraries. So how can I add this functionality programmatically to my components?

       

      Thank you...

        • 1. Re: How to add a4j:support programmatically in richfaces 4
          Ilya Shaikovsky Master

          http://java.dzone.com/articles/jsf-2-client-behaviors

           

          read ClientBehaviorHolder section.

          1 of 1 people found this helpful
          • 2. Re: How to add a4j:support programmatically in richfaces 4
            Chris Mathrusse Newbie

            Thanks for the reply, Ilya. I did read through this section yesterday prior to posting but had issues getting it to work and still do for that matter. I'm attempting to render the UI entirely from Java, representing data within our database. The issue I have is the following:

             

            HtmlSelectOneRadio radio = new HtmlSelectOneRadio();  radio.addClientBehavior("onselect", new SelectChoiceBehavior(managedBeanName)); // After adding the client behavior I should be able to get the behaviors radio.getClientBehaviors(); // but this is the result after adding the behavior above // (java.util.Collections$EmptyMap) {}

             

            I was able to add script in the onclick method, which is what I am attempting to do with the behavior. I copied the example from the link you sent me and followed all the instructions. But after I add the behavior it appears that it is not there. So what am I doing wrong?

             

            My intent is to perform a callback to make an assignment when a radio button is clicked.

             

            Thanks for the help...

            • 3. Re: How to add a4j:support programmatically in richfaces 4
              Nick Belaevski Master

              Hi Chris,

               

              HtmlSelectOneRadio doesn't declare "onselect" ass supported event name. You should be using "select" instead.

              • 4. Re: How to add a4j:support programmatically in richfaces 4
                Chris Mathrusse Newbie

                http://www.crunchgear.com/wp-content/uploads/2009/03/24819bpthe-simpsons-homer-d-oh-posters.jpg

                Well, that seemed to work out a bit better than before. (I actually wanted to use the click event)

                 

                Now my only question remaining is that I want the 'click' event to execute an Ajax call back to the server to perform an assignment to a managed bean. I understand that I want to provide this behavior in my getScript method of my behavior class, but I'm uncertain as to how this should be scripted. I tried the following:

                 

                ValueExpression ve =

                     behaviorContext.getFacesContext().getApplication().getExpressionFactory()

                .createValueExpression(behaviorContext.getFacesContext().getELContext(), "#{" + managedBean + ".choice}",this.getClass());

                 

                But this only resulted in the following script being generated on the client.

                 

                <input type="radio" name="form:answer-2" id="form:answer-2:3" value="4" onclick="#{choiceSelect2.choice}"/>
                
                

                Which is no precisely what I had in mind. How should the script look for performing a callback to my managed bean?

                 

                Thanks again... you've been a big help.

                • 5. Re: How to add a4j:support programmatically in richfaces 4
                  Ilya Shaikovsky Master

                  as I guess you need to attach server side action.. So better to show once than describe in a few answers

                   

                   

                  <h:form>
                  <a4j:commandButton render="inputdiv" value="create" action="#{dynamicAjax.createAjaxInput}"/>
                  <a4j:outputPanel id="inputdiv">
                  <a4j:outputPanel binding="#{dynamicAjax.panel}">
                   
                  </a4j:outputPanel>
                  </a4j:outputPanel>
                  </h:form>
                  

                   

                  package org.richfaces.demo.common;
                  
                  import javax.el.MethodExpression;
                  import javax.faces.bean.ManagedBean;
                  import javax.faces.bean.RequestScoped;
                  import javax.faces.component.html.HtmlInputText;
                  import javax.faces.context.FacesContext;
                  import javax.faces.event.MethodExpressionValueChangeListener;
                  import javax.faces.event.ValueChangeEvent;
                  
                  import org.ajax4jsf.component.behavior.AjaxBehavior;
                  import org.richfaces.component.UIOutputPanel;
                  
                  
                  @ManagedBean
                  @RequestScoped
                  public class DynamicAjax {
                      UIOutputPanel panel;;
                  
                      public void createAjaxInput() {
                          HtmlInputText input = (HtmlInputText) FacesContext.getCurrentInstance().getApplication().createComponent(HtmlInputText.COMPONENT_TYPE);
                          AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(
                              AjaxBehavior.BEHAVIOR_ID);
                  
                          MethodExpression listener = FacesContext.getCurrentInstance().getApplication().getExpressionFactory()
                              .createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{dynamicAjax.method}", null,
                                  new Class[] { ValueChangeEvent.class });
                          MethodExpressionValueChangeListener valueChangeListener = new MethodExpressionValueChangeListener(listener);
                          input.addValueChangeListener(valueChangeListener);
                          input.addClientBehavior("change", ajaxBehavior);
                          panel.getChildren().add(input);
                      }
                  
                      public void method(ValueChangeEvent changeEvent) {
                          System.out.println("DynamicAjax.method()");
                      }
                  
                      public UIOutputPanel getPanel() {
                          return panel;
                      }
                  
                      public void setPanel(UIOutputPanel panel) {
                          this.panel = panel;
                      }
                  
                  
                  
                   }
                   
                  

                   

                  works fine for me and I see systrace in console after new input changed and blured.

                  1 of 1 people found this helpful
                  • 6. Re: How to add a4j:support programmatically in richfaces 4
                    Chris Mathrusse Newbie

                    Great example. Thank you.

                     

                    I copied your sample code into my project to give it a try. I was unable to get the new input and blur events to invoke the method with a ValueChangeEvent. The only time it seems to be invoked is when the create button is pressed. But this does steer me in the right direction. I'll keep working with it to see if I can get it to work as expected. Perhaps I don't have something configured correctly within my project.

                     

                    Thanks again...

                    • 7. Re: How to add a4j:support programmatically in richfaces 4
                      Marcio Justo Newbie

                      I tryed to do the same with a HtmlSelectOneMenu, but it doens't work. It not call the method that I created like the example. Could you help me with that.

                       

                      Thanks.