Setter not called for input text

て烟熏妆下的殇ゞ 提交于 2019-12-08 04:23:56

问题


I have a form which has a input text and a set of radio buttons. When a radio button is clicked I am invoking a value change listener. Inside the value change listener I am printing the value user has entered in the input text field. But I always get the older value of the text field and not the new value which the user is entering. I understand that ValueChangeListener comes under the Validation cycle. But if I need to access the new value of the input field what should I do? Note: The managed bean is request scope. If I change the scope to session, it works fine. Any explanation on this would be welcome. The code in JSP is as below:

<h:form>
Enter name:<h:inputText value="#{employee.empId}"></h:inputText>
Choose option: <h:selectOneRadio onclick="this.form.submit()" 
            valueChangeListener="#{employee.check}" >
<f:selectItem itemLabel="one" itemValue="one"/>
<f:selectItem itemLabel="two" itemValue="two"/>
</h:selectOneRadio>
</h:form>

回答1:


Your problem description (old value only available in request scope; and it "works" in session scope) matches with the case as if you would be accessing the property directly instead of getting it from the ValueChangeEvent. This is indeed not right.

The ValueChangeEvent offers you getters to return the old and the new value. You should use it instead of accessing the property directly.

public void check(ValueChangeEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();
    // ...
}

Update: as per comments, you're actually interested in the value of the input field. This is only been set during update model values phase, which is after the validations phase, when the value change listener runs.

Given that you're using legacy JSF 1.2 and thus can't use the JSF2 ajax awesomeness, then one of the ways to solve this is to manually queue the value change event to the invoke action phase so that you can get the submitted input text value.

public void check(ValueChangeEvent event) {
    if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }

    System.out.println(empId); // It's available in here.
}    


来源:https://stackoverflow.com/questions/13227667/setter-not-called-for-input-text

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