【发布时间】:2013-07-31 10:05:46
【问题描述】:
我正在学习如何在 jsf 中使用 ajax,我创建了一个实际上什么都不做的页面,一个用数字填充的输入文本,提交到服务器,使用提交的值调用该元素的设置器,然后显示 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 时,会正常调用。
【问题讨论】:
标签: ajax jsf jsf-2 form-submit