JSF addvaluechangeListener not executed when value changed for dynamically created selectonemenu

I am trying to add a valuechange Listener to dynamically created selectonemenu.Below is the code:

SelectOneMenu selectmsgtype = new SelectOneMenu();
UISelectItems msgtypeitems = new UISelectItems();
final String selecttypeBinding = "#{bean.selectmsgtypehash.key"
            + compCount + "}";
final ValueExpression selecttypeValueExp = FacesContext
            .getCurrentInstance()
            .getApplication()
            .getExpressionFactory()
            .createValueExpression(getELContext(), selecttypeBinding,
                    Integer.class);
selectmsgtype.setValueExpression("value", selecttypeValueExp);
selectmsgtype.addValueChangeListener(valueChangeListener);
selectmsgtype.setImmediate(true);
selectmsgtype.getChildren().add(msgtypeitems);
component.getChildren().add(selectmsgtype);*

Here valueChangeListener is a managedproperty

public class CustomValueChangeEvent implements ValueChangeListener  {

@Override
public void processValueChange(ValueChangeEvent arg0)
        throws AbortProcessingException {
    SelectOneMenu component1=(SelectOneMenu) arg0.getComponent();
    System.out.println("Valuechange"+component1+"\nId"+component1.getId());
//  component1.getId();

    }
}

Can anyone let me know what is missing in the above so that the valuechange listener gets executed on change of value? I am using jsf 2.0 and primefaces

Answers


Could be either or both of two things:

  1. You're not setting an id for the dynamically created component: I don't see where you're setting the id attribute for the dynamically created component. You need to do this by yourself (JSF won't do it for you because you've taken control of the component creation)

    selectmsgtype.setId(`theId`); 
    
  2. How you're calling the listener: Typically, a valueChangeListener runs only when a full (non-ajax) request is sent to the server. You're not showing where (or if) you're firing the request.


EDIT: Based on your comments, you don't need a ValueChangeListener, you need a plain ajax listener. Attach an ajax listener to your dynamically created component using the snippet below:

AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
ajaxBehavior.addAjaxBehaviorListener(new CustomAjaxListener());
ajaxBehavior.setTransient(true);
selectmsgtype.addClientBehavior("change",ajaxBehavior);

CustomAjaxListener is going to be an implementation of AjaxBehaviorListener:

public class CustomAjaxListener implements AjaxBehaviorListener {

@Override
public void processAjaxBehavior(AjaxBehaviorEvent event) throws AbortProcessingException {
   SelectOneMenu component1=(SelectOneMenu) event.getComponent();
   System.out.println("Valuechange"+component1+"\nId"+component1.getId());
  }
}

Need Your Help

Issues calling a DLL from WCF/Silverlight

silverlight-4.0 windows-server-2003 wcf-ria-services

Trying to use a DLL that returns a list of tasks in the W2k3 server's Task Scheduler. Works great when I use it in a C# console app on the server, and using it on my computer (ASP.NET Dev Server), ...

How to parse nested JSON arrays in Objective C

php objective-c arrays json parsing

I have this JSON tree and I need to group the arrays by category key. (In this example,: "","1","2","3" and "serf" are value names for categories.) I'm serializing JSON using NSJSONSerialization bu...