我正在学习如何在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.0 – Ajax (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 举报,一经查实,本站将立刻删除。