JSTL c:forEach 标记不适用于 JSF 中的 El 表达式
JSTL c:forEach tag not working for El expression in JSF
在尝试使用 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>
内容_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
(只有前 2 个测试有效: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
我最终发现它实际上与配置无关,但它与构建时间和渲染时间有关(这就是导致我做 c:forEach 而不是 ui:repeat,像我一样需要 ui:include)。
因为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>
在尝试使用 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>
内容_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 (只有前 2 个测试有效: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
我最终发现它实际上与配置无关,但它与构建时间和渲染时间有关(这就是导致我做 c:forEach 而不是 ui:repeat,像我一样需要 ui:include)。
因为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>