设置器未要求输入文本 [英] Setter not called for input text

查看:129
本文介绍了设置器未要求输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有输入文本和一组单选按钮的表格.单击单选按钮时,我正在调用一个值更改侦听器.在值更改侦听器中,我正在打印用户在输入文本字段中输入的值.但是我总是得到文本字段的旧值,而不是用户输入的新值.我了解ValueChangeListener属于验证周期.但是,如果我需要访问输入字段的新值,该怎么办? 注意:托管bean是请求范围.如果我将范围更改为会话,则可以正常工作.任何对此的解释都将受到欢迎. JSP中的代码如下:

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>

推荐答案

您的问题描述(旧值仅在请求范围内可用;它在会话范围内起作用")与情况匹配,就好像您将访问该属性一样而不是直接从 ValueChangeEvent .这确实是不对的.

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.

ValueChangeEvent为您提供了获取方法,以返回旧值和新值.您应该使用它,而不是直接访问该属性.

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.

鉴于您正在使用旧版JSF 1.2,因此无法使用JSF2 ajax出色功能,那么解决此问题的方法之一是将值更改事件手动排队到invoke action阶段,以便您可以提交的输入文本值.

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.
}    

这篇关于设置器未要求输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆