添加< f:ajax>时,提交的表单值未在模型中更新到< h:commandButton> [英] Submitted form values not updated in model when adding <f:ajax> to <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.
推荐答案
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>
另请参见:
- JSF 2.0中的通信- Ajax(异步)POST表单
- commandButton/commandLink/ajax action/listener方法未调用或输入值未更新(第8点)
- 了解PrimeFaces流程/更新和JSF f:ajax执行/渲染属性
- Communication in JSF 2.0 - Ajax (asynchronous) POST form
- commandButton/commandLink/ajax action/listener method not invoked or input value not updated (point 8)
- Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
See also:
这篇关于添加< f:ajax>时,提交的表单值未在模型中更新到< h:commandButton>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!