原来的问题在下面,但我已经提出了一个更小的例子来演示这个问题,并认为它应该放在顶部。
无论如何,在检查父元素是否实际呈现之前,似乎处理了ui:repeat
标记。要重新创建它,下面是facelet (minimalTest.xhtml):
<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>Test JSF <ui:repeat> inside <h:panelGroup rendered="false"></title>
</h:head>
<h:body>
<h:form>
<h1>Testing</h1>
<h:panelGroup rendered="false">
<span>#{minimalTestBean.alsoThrowsException}</span>
<ul>
<ui:repeat value="#{minimalTestBean.throwsException}" var="item">
<li>#{item}</li>
</ui:repeat>
</ul>
</h:panelGroup>
</h:form>
</h:body>
</html>
使用这个bean (MinimalTestBean.java):
package com.lucastheisen.beans;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class MinimalTestBean implements Serializable {
private static final long serialVersionUID = 9045030165653014015L;
public String getAlsoThrowsException() {
throw new RuntimeException( "rendered is false so this shouldnt get called either" );
}
public List<String> getThrowsException() {
throw new RuntimeException( "rendered is false so this shouldnt get called" );
}
}
从这个例子中,您可以看到包含ui:repeat
的h:panelGroup
被静态地设置为rendered=false
,我认为这意味着h:panelGroup
内部的EL表达式都不会被执行。EL表达式只调用会抛出RuntimeException的getter。然而,ui:repeat
实际上是在为它的列表调用getter,从而导致异常,即使它一开始不应该被呈现。如果注释掉ui:repeat
元素,则不会抛出异常(即使其他EL表达式仍保留在h:panelGroup
中),正如我所期望的那样。
在stackoverflow上阅读其他问题让我相信这可能与经常提到的鸡/蛋问题有关,但我不确定确切的原因,也不知道该怎么做。我认为将PARTIAL_STATE_SAVING设置为false可能会有所帮助,但希望避免内存影响。
----原题----
基本上,我有一个页面,有条件地呈现部分使用<h:panelGroup rendered="#{modeXXX}">
围绕<ui:include src="pageXXX.xhtml" />
(根据这个答案)。问题是,如果其中一个pageXXX.xhtml中有一个<ui:repeat>
,那么即使包含<h:panelGroup>
的rendered=false
也会被处理。这是一个问题,因为我的一些节依赖于在它们之前应该访问的其他节的初始化。为什么要处理所包含的pageXXX.xhtml ?
这是一个令人痛苦的错误,难以置信地难以将其归结为一个小示例,但这里是我可以构建的演示该问题的最小案例。第一个基本页:
<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>Test JSF <ui:include></title>
</h:head>
<h:body>
<h:form>
<h1>#{testBean.title}</h1>
<h:panelGroup rendered="#{testBean.modeOne}">
<ui:include src="modeOne.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{testBean.modeTwo}">
<ui:include src="modeTwo.xhtml" />
</h:panelGroup>
</h:form>
</h:body>
</html>
如您所见,该页将根据testBean bean中的值有条件地包括modeOne页或modeTwo页。然后是modeOne(默认):
<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">
<ui:composition>
<span>Okay, I'm ready. Take me to </span>
<h:commandLink action="#{testBean.setModeTwo}">mode two.</h:commandLink>
</ui:composition>
</html>
在我的现实世界的应用程序将是一个页面,设置所需的modeTwo。设置完成后,该页面上的操作将引导您到modeTwo:
<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">
<ui:composition>
<div>Here is your list:</div>
<ui:repeat value="#{testBeanToo.list}" var="item">
<div>#{item}</div>
</ui:repeat>
</ui:composition>
</html>
modeTwo页面基本上在ui中呈现了modeOne页面的详细信息:与集合中的实际信息一样重复。主托管bean (TestBean):
package test.lucastheisen.beans;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 6542086191355916513L;
private Mode mode;
@ManagedProperty( value="#{testBeanToo}" )
private TestBeanToo testBeanToo;
public TestBean() {
System.out.println( "constructing TestBean" );
setModeOne();
}
public String getTitle() {
System.out.println( "ttb.getTitle()" );
return mode.getTitle();
}
public boolean isModeOne() {
return mode == Mode.One;
}
public boolean isModeTwo() {
return mode == Mode.Two;
}
public void setModeOne() {
this.mode = Mode.One;
}
public void setModeTwo() {
testBeanToo.getReadyCauseHereICome();
this.mode = Mode.Two;
}
public void setTestBeanToo( TestBeanToo testBeanToo ) {
this.testBeanToo = testBeanToo;
}
private enum Mode {
One("Mode One"),
Two("Mode Two");
private String title;
private Mode( String title ) {
this.title = title;
}
public String getTitle() {
return title;
}
}
}
是所有主数据的bean,而TestBeanToo bean将用于详细信息:
package test.lucastheisen.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestBeanToo implements Serializable {
private static final long serialVersionUID = 6542086191355916513L;
private ObjectWithList objectWithList = null;
public TestBeanToo() {
System.out.println( "constructing TestBeanToo" );
}
public String getTitle() {
System.out.println( "ttb2.getTitle()" );
return "Test Too";
}
public List<String> getList() {
System.out.println( "ttb2.getList()" );
return objectWithList.getList();
}
public void getReadyCauseHereICome() {
System.out.println( "ttb2.getList()" );
objectWithList = new ObjectWithList();
}
public class ObjectWithList {
private List<String> list;
public ObjectWithList() {
list = new ArrayList<String>();
list.add( "List item 1" );
list.add( "List item 2" );
}
public List<String> getList() {
return list;
}
}
}
<ui:repeat>
不检查自己的rendered
属性(它实际上没有)和它的父视图呈现时。考虑使用战斧的<t:dataList>
。