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

我正在学习如何在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


html5中section的用法
span标签和p标签的区别
jsp文件和html文件的区别是什么
span标签和div的区别
html颜色代码表大全
span标签的作用是什么
dhtml的主要组成部分包括什么
html编辑器哪个软件好用
span标签属于什么样式标签
html文件乱码怎么办
html怎么读取json文件
html文件打开乱码怎么恢复原状
html怎么链接外部css
html文件怎么保存到本地
html怎么链接css文件
html和css怎么连接
html和css怎么关联
html文件怎么保存到一个站点
html文件怎么写
html出现乱码怎么解决