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

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

问题描述

我正在学习如何在jsf中使用ajax,我制作了一个实际上不执行任何操作的页面,将填充有数字的输入文本提交给服务器,并使用提交的值调用该元素的setter,然后显示吸气剂的值.

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.

当我取下二传手时,通常称为.

When I remove the the setter is called normally.

推荐答案

默认情况下,<f:ajax>仅处理当前组件(

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>

实际上,仅处理<h:commandButton action>,而完全忽略了<h:inputText value>(以及任何其他输入字段,如果有的话).

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天全站免登陆