user3094040
user3094040

Reputation: 21

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

Upvotes: 2

Views: 1301

Answers (1)

kolossus
kolossus

Reputation: 20691

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());
  }
}

Upvotes: 1

Related Questions