添加到时,提交的表单值未在模型中更新

我正在学习如何在jsf中使用ajax,我创建了一个实际上什么也没做的页面,一个填充了数字的输入文本,提交给服务器,使用提交的值调用该元素的setter,并显示getter的值.

这是简单bean的代码

@ManagedBean(name="helper",eager=true)
public class HealthPlanHelper {


    String random = "1";

    public void setRandomize(String s){
        random = s;
                System.out.println("Calling setter");
    }

    public String getRandomize(){
        return random;
    }

}

和jsf页面

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>

    <h:form>
        <h:commandButton action="nothing">
            <f:ajax render="num"/>
        </h:commandButton>

        <h:inputText value="#{helper.randomize}" id="num"/>
    </h:form>

</h:body>
</html>

如您所见,这是一个请求范围的bean,每当我单击按钮时,服务器显示它创建了bean的实例,但是从不调用setter方法,因此,getter总是返回“1”作为值的串.

当我删除时,正常调用setter.

解决方法

< f:ajax>认情况下,只处理当前组件( read description of execute attribute).基本上,您的代码与此完全相同:
<h:form>
    <h:commandButton action="nothing">
        <f:ajax execute="@this" render="num"/>
    </h:commandButton>
    <h:inputText value="#{helper.randomize}" id="num"/>
</h:form>

效果中,只有< h:commandButton动作>已处理并且< h:inputText值> (以及任何其他输入字段,如果有的话)被完全忽略.

您需要更改execute属性以显式指定在ajax请求期间要处理的组件或部分.通常,为了处理整个表单,使用了@form:

<h:form>
    <h:commandButton action="nothing">
        <f:ajax execute="@form" render="num"/>
    </h:commandButton>
    <h:inputText value="#{helper.randomize}" id="num"/>
</h:form>

也可以看看:

> Communication in JSF 2.0Ajax (asynchronous) POST form
> commandButton/commandLink/ajax action/listener method not invoked or input value not updated(第8点)
> Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

相关文章

Mip是什么意思以及作用有哪些
怎么测试Mip页面运行情况
MIP安装的具体步骤有哪些
HTML添加超链接、锚点的方法及作用详解(附视频)
MIP的规则有哪些
Mip轮播图组件中的重要属性讲解