【发布时间】:2011-09-29 16:49:48
【问题描述】:
我在这里和网络上搜索并尝试了各种东西; prependId,使用完整的 id 路径(:form:panelid:componentid 有点像)等。
我还是一头雾水。我还是个 JSF 菜鸟 :)
我的问题是,我在 f:ajax 渲染部分中指定的内容并不是 唯一 被“执行”的部分。我的意思是,在 render 中指定的 id 不会在屏幕上重新渲染,但该组件的值会被调用。
所以:
inputText 的 f:ajax render=outA event=blur
当模糊发生时,outA 的 getter 被调用并重新渲染,但其他组件的 getter 也被调用,但它们不被重新渲染。
具体来说,根据下面列出的代码:
A) 当 val1 失去焦点时,会触发 blur 事件,它会调用 getValue1() 并重新渲染 outval1。快乐的。但是,getStuff() 也被调用(它也发生在其他模糊和 btnCalc 中)但是 getStuff() 的结果不会呈现到 tblStuff dataTable。 为什么?
我该如何解决它,以便在 blur 事件中,只执行与 render=".." 组件相关的 getter?
我应该使用不同的部分吗?
B) 当单击刷新按钮时,它会再次调用 getStuff,现在显示新的 dataTable / 即在模糊事件和 btnReload 单击事件期间添加的所有 ValueTO 的新数据。
C) 对于在 A 中命名的任何一个事件,getStuff 方法被准确地调用了 8 次。即点击输入框内,点击输入框外,getStuff() * 8。 然而,getValue1 只被调用了两次?
D) 有什么好的 JSF2 书籍吗?
这是页面:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Stuff</title>
</h:head>
<h:body>
<h:form id="frmTrxMain" prependId="true">
<h1>
Stuff
</h1>
<h:dataTable title="Stuff" id="tblStuff" var="s" value="#{bean.stuff}" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{s.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{s.name}" />
</h:column>
</h:dataTable>
<h:outputText value="No stuff to display" rendered="#{empty bean.stuff}"/>
<h:commandButton value="Refresh" id="btnReload">
<f:ajax render="tblStuff" event="click"/>
</h:commandButton>
<hr/>
<h:panelGrid columns="3" id="pnlTwo">
<h:outputLabel value="Value1"/>
<h:inputText value="#{bean.value1}" id="val1">
<f:ajax event="blur" render="outVal1"/>
</h:inputText>
<h:outputText id="outVal1" value="Entered: #{bean.value1}" />
<h:outputLabel value="Value2"/>
<h:inputText value="#{bean.value2}" id="val2">
<f:ajax event="blur" render="outVal2"/>
</h:inputText>
<h:outputText id="outVal2" value="Entered: #{bean.value2}" />
<h:commandButton value="Calc" id="btnCalc">
<f:ajax event="click" render="outSum"/>
</h:commandButton>
</h:panelGrid>
<h:outputLabel id="outSum" value="#{bean.sum}"/>
</h:form>
</h:body>
</html>
还有后台bean:
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "bean")
@SessionScoped
public class TestStuffBean {
private int id=1;
private String name="a";
//test.xhtml
private String value1;
private String value2;
private List<TestVO> stuff = new ArrayList<TestVO>();
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getSum() {
String result = "";
int sum;
try {
sum = Integer.parseInt(value1) + Integer.parseInt(value2);
result = Integer.toString(sum);
} catch (NumberFormatException e) {
result = "Enter two integers";
}
return result;
}
public List<TestVO> getStuff() {
//the VO is just a POJO with a contructor that takes
//two strings and a getter/setter for each id and name
stuff.add(new TestVO(Integer.toString(id), name+id));
id++;
return stuff;
}
}
【问题讨论】:
标签: java ajax jsf jsf-2 render