valueChangeListener is invoked on submit without changing options in selectOneMenu

梦想与她 提交于 2020-01-17 03:00:29

问题


In my page I have selectonemenu and I have noticed that when I click the pagination of datatable, valueChangeListener method is getting invoked without actually changing values or options in selectonemenu.

How is this happening and how could I prevent this from happening as I need to invoke valueChangeListener method only if user changes values in selectonemenu.

Thanks and appreciate any help.

Regards

Here is my code for selectonemenu

Update1

<ice:selectOneMenu value="#{list.selectedItem}"
            partialSubmit="true" valueChangeListener="#{list.val}">
                <f:selectItems value="#{list.selectItems}" />
                </ice:selectOneMenu>

And bean method

   public void val(ValueChangeEvent event) {
            logger.info("1");
            selectedValue = event.getNewValue().toString();
}

Values are added like

public void loadItems(){
        selectItems.add(new SelectItem("1", "One"));
        selectItems.add(new SelectItem("2", "Two"));
        selectItems.add(new SelectItem("3", "Three"));
        selectItems.add(new SelectItem("4", "Four")); 

}

And method is called as follows

 public List<SelectItem> getSelectItems() {
        if (selectItems == null) {
            loadItems;

        }
        return selectItems;
    }

回答1:


The JSF valueChangeListener is in no way related to the client-side events. It is a server-side event which will be invoked when the submitted value differs from the initial value.

The first item of your list has a value of 1, so when the user doesn't change the dropdown, the submitted value will be 1. If you don't want the valueChangeListener to be invoked, then you need to ensure that the initial value also equals to 1. That is the value behind #{list.selectedItem}". Initialize it to the same value as the first item during bean's construction/initialization.

this.selectedItem = "1";


来源:https://stackoverflow.com/questions/4932810/valuechangelistener-is-invoked-on-submit-without-changing-options-in-selectoneme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!