在尝试使用JSF解决更大的问题时,我注意到我似乎无法使用c:forEach遍历列表。研究告诉我,现在有几种不同的版本,我相信我正在使用JSF 2.2。
我在web.xml和xmlns:c标记中尝试了一些不同的设置,但没有结果。
以下是一个测试文件,用于演示无效的内容: index.html包括Content / Content_index.xhtml,其中包括Content / Recursive_Box.xhtml:
index.xhtml
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<c:choose>
<c:when test="#{LoginBean.currentUser.loggedOn}">
<ui:include src="#{MainBean.setLayout(0,0,0)}"></ui:include>
</c:when>
<c:otherwise>
<ui:include src="#{MainBean.setLayout(0,6,0)}"></ui:include>
</c:otherwise>
</c:choose>
</html>
Content_index.xhtml
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<c:choose>
<c:when test="#{LoginBean.currentUser.loggedOn}">
<title>
Bubble Up!
</title>
<!--<c:set value="#{TargetBean.PullStructure(LoginBean.currentUser.userIndex)}" var="tickle"></c:set>-->
<ui:include src="Recursive_Box.xhtml">
<ui:param name="box" value="#{TargetBean.structure}" />
</ui:include>
</c:when>
</c:choose>
</html>
Recursive_Box.xhtml (只有前两个测试有效:Static和Test1)
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
#{box}<br/>
#{box.boxList}<br/>
#{box.getBoxList()}<br/>
#{box.boxList.get(0)}<br/>
#{box.boxList.size()}<br/>
<c:forEach var = "i" begin = "1" end = "5">
Static<br/>
</c:forEach>
<c:forEach var = "i" begin = "0" end = "#{box.boxList.size()}">
Test1<br/>
</c:forEach>
<c:forEach var = "i" begin = "1" end = "#{box.boxList.size()}">
Test2<br/>
</c:forEach>
<c:forEach var = "i" items = "#{box.getBoxList()}">
Test2<br/>
</c:forEach>
<c:forEach var = "i" begin = "1" end = "#{box.getBoxList().size()}">
Test3<br/>
</c:forEach>
<c:forEach var = "i" begin = "1" end = "${box.boxList.size()}">
Test4<br/>
</c:forEach>
<c:forEach var = "i" items = "${box.getBoxList()}">
Test5<br/>
</c:forEach>
<c:forEach var = "i" begin = "1" end = "${box.getBoxList().size()}">
Test6<br/>
</c:forEach>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
我应该能够遍历给定的列表,但是对于我的测试,我得到以下输出:
Main.Box@50eb0e0
[Main.Box@2452c4d, Main.Box@5fd03294, Main.Box@4c97b69b, Main.Box@3c6e8659, Main.Box@17e2e2dd]
[Main.Box@2452c4d, Main.Box@5fd03294, Main.Box@4c97b69b, Main.Box@3c6e8659, Main.Box@17e2e2dd]
Main.Box@2452c4d
5
Static
Static
Static
Static
Static
Test1
答案 0 :(得分:0)
我最终发现它实际上与配置无关,但与构建与渲染时间有关(这就是导致我执行c:forEach而不是ui:repeat的原因,需要一个ui:包括我的操作)。
由于TargetBean(集合所在的位置)依赖于LoginBean,因此必须使用LoginBean的用户ID构造TargetBean,以从数据库中获取集合。这也必须是发生的第一件事,以便c:forEach循环具有要迭代的信息,因此也必须在构建期间发生(例如c:forEach),因此我不得不将调用放在第一个c:forEach中因为我没有其他构建时标记用于示例。
这是我的测试文件,可以显示结果:
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
Test 1:<br/>
<c:forEach var = "i" begin = "0" end = "#{TargetBean.PullStructure(0).boxList.size()-1}">
Result 1!<br/>
</c:forEach>
#{TargetBean}<br/>
#{TargetBean.structure.boxList}<br/>
#{TargetBean.structure.getBoxList()}<br/>
#{TargetBean.structure.boxList.get(0)}<br/>
#{TargetBean.structure.boxList.size()}<br/>
<br/><br/>
Static:<br/>
<c:forEach var = "i" begin = "0" end = "4">
#{i}: #{TargetBean.structure.boxList.get(i)}<br/>
</c:forEach>
Test 2:<br/>
<c:forEach var = "i" begin = "1" end = "#{TargetBean.structure.boxList.size()-1}">
Result 2!<br/>
</c:forEach>
Test 3:<br/>
<c:forEach var = "i" items = "#{TargetBean.structure.getBoxList()}">
Result 3!<br/>
</c:forEach>
Test 4:<br/>
<c:forEach var = "i" begin = "1" end = "#{TargetBean.structure.getBoxList().size()-1}">
Result 4!<br/>
</c:forEach>
Test 5:<br/>
<c:forEach var = "i" begin = "1" end = "${TargetBean.structure.boxList.size()-1}">
Result 5!<br/>
</c:forEach>
Test 6:<br/>
<c:forEach var = "i" items = "${TargetBean.structure.getBoxList()}">
Result 6!<br/>
</c:forEach>
<c:forEach var = "i" begin = "1" end = "${TargetBean.structure.getBoxList().size()-1}">
Test6<br/>
</c:forEach>
Test 7:<br/>
<c:forEach var = "i" begin = "0" end = "${TargetBean.structure.boxList.size()-1}">
#{TargetBean.structure.boxList.get(i)}<br/>
</c:forEach>
</html>