How to detect a field value change in a JSF 2.0 page

时光怂恿深爱的人放手 提交于 2019-12-21 17:05:30

问题


I am using JSF 2.0 to develop a pretty big and complex page which contains numerous fields. Thre would be quit command button at the buttom of the page and when ever user selects the quit option I need to detect whether user has entered any value on one of the fields of the page.

I am using the null check of each field values in the backing bean to do that now, but that's a very tedious and repeative job. I was wondering is there any smart solution for that ?? Any help will be highly appreciated.

Thanks in advance.


回答1:


For that the valueChangeListener attribute is meant to be used.

<h:inputText ... valueChangeListener="#{bean.changed}" />

with

public void changed(ValueChangeEvent event) {
    this.changed = true;
}



回答2:


Your field-values are probably linked to properties of the backing-bean. When the value is changed, the setter is invoked. Inside the setter you could set a boolean field of the bean to true, if the value actually changed.

public void setPropertyX(Type newValue) {
    if(!newValue.equals(this.X)) {
        this.X = newValue;
        this.fieldChanged = true;
    }
}

For this to work, you backing-bean should be at least in @ViewScope.



来源:https://stackoverflow.com/questions/9563527/how-to-detect-a-field-value-change-in-a-jsf-2-0-page

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