添加 &lt;f:ajax&gt; 时,提交的表单值未在模型中更新<h:commandButton> [英] Submitted form values not updated in model when adding &lt;f:ajax&gt; to &lt;h:commandButton&gt;

查看:37
本文介绍了添加 &lt;f:ajax&gt; 时,提交的表单值未在模型中更新<h:commandButton>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning how to use ajax within jsf, I made a page that does actually nothing, an input text that is filled with a number, submitted to the server, call the setter for that element with the value submitted, and display the getter's value.

这是简单的 bean 代码:

Here's the simple bean's code:

@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"作为字符串的值.

As you see, this is a request scoped bean, whenever I click the button the server shows that it creates an instance of the bean, but the setter method is never called, thus, the getter return always "1" as the value of the string.

当我删除 setter 时正常调用.

When I remove the the setter is called normally.

推荐答案

默认只处理当前组件 (阅读execute 属性的描述).基本上,您的代码与此完全相同:

The <f:ajax> processes by default only the current component (read description of execute attribute). Basically, your code is exactly the same as this:

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

实际上,只有 被处理,(和任何其他输入字段,如果有的话)) 被完全忽略.

In effects, only the <h:commandButton action> is been processed and the <h:inputText value> (and any other input field, if any) is completely ignored.

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

You need to change the execute attribute to explicitly specify components or sections you'd like to process during the ajax request. Usually, in order to process the entire form, @form is been used:

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

另见:

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