Java 在 JSF 中使用 foreach 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20468236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using foreach loop in JSF
提问by user3058296
I need to create a list of commandbuttons on my page, but I have some trouble with it. I pass the list from my bean through request.setAttribute and it seems to work when I get the values one at a time, but when I run through a foreach loop, they all seem to be null (And thereby being generated, and having a default value 0, "" etc., as far as I know). Any help would be greatly appreciated! In the added code, I get the correct values when i make the buttons outside of the foreach loop, but not when I run the loop itself. The List is of integer type, and should later be a java object (ran into same problem). using JSF version 2.2. Both logtest() and gotoprofile() prints the interestpointer out.
我需要在我的页面上创建一个命令按钮列表,但我遇到了一些麻烦。我通过 request.setAttribute 从我的 bean 传递列表,当我一次获取一个值时它似乎可以工作,但是当我通过 foreach 循环运行时,它们似乎都为空(因此被生成,并有一个默认值 0、"" 等,据我所知)。任何帮助将不胜感激!在添加的代码中,当我在 foreach 循环之外制作按钮时,我得到了正确的值,但在我运行循环本身时却没有。List 是整数类型,以后应该是一个 java 对象(遇到同样的问题)。使用 JSF 2.2 版。logtest() 和 gotoprofile() 都打印出兴趣指针。
my bean has:
我的豆子有:
@ManagedBean(name="MyProfile")
@RequestScoped
And I set my variable myInterestList in my bean with:
我在我的 bean 中设置了我的变量 myInterestList:
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
session.setAttribute("myInterestProfileName", profileName);
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ page import="java.util.List,com.jsflogin.stringWithPointer" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSF Successfull login</title>
</head>
<body>
<h:form id="forloop">
<c:set var ="myTempList" value="${myInterestListYay}" scope="session"/>
<c:out value="interest list"/><p>
<h:commandButton value="#{myInterestListYay[1]}" action="#{MyProfile.logTest}">
<f:setPropertyActionListener target ="#{MyProfile.interestPointer}" value = "#{myInterestListYay[1]}"/>
</h:commandButton><p>
<ui:repeat var="orly"value="${myInterestListYay}" varstatus="status">
<c:out value="${status.index}"/><h:commandButton value="#{orly}" action="#{MyProfile.logTest}">
<f:setPropertyActionListener target ="#{MyProfile.interestPointer}" value = "#{orly}"/>
</h:commandButton><p>
</ui:repeat>
<c:forEach var="orly" items="${MyProfile.interestsAndPointers}" varStatus="status" >
<c:out value="${status.index}"/><c:out value=": "/><c:out value="${orly.stringName}"/><h:commandButton value="go to interest page" action="#{MyProfile.goToInterestProfile}">
<f:setPropertyActionListener target ="#{MyProfile.interestPointer}" value = "#{orly.pointer}"/>
</h:commandButton><p>
</c:forEach>
</h:form>
</body>
</f:view>
</html>
采纳答案by Spindizzy
If you are using JSF 2 you should change your page to xhtml, then you can use ui:repeatand get a lot more goodies from facelets.
如果您使用的是 JSF 2,您应该将您的页面更改为 xhtml,然后您可以使用ui:repeat并从 facelets 中获得更多好东西。
I made two very simple pages, one as JSP and the other one as XHTML. They use a managed bean in request scope. Both are working and render three buttons in a row. Note that I use Glassfish as a server, since it was easier to getting started. For the Tomcat(7.x) you might need to copy the jsf-api, jsf-impl (2.x) and jstl (1.2) libs into the classpath.
我制作了两个非常简单的页面,一个是 JSP,另一个是 XHTML。他们在请求范围内使用托管 bean。两者都在工作并连续渲染三个按钮。请注意,我使用 Glassfish 作为服务器,因为它更容易上手。对于 Tomcat(7.x),您可能需要将 jsf-api、jsf-impl (2.x) 和 jstl (1.2) 库复制到类路径中。
This is the JSP page:
这是 JSP 页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h:form>
<c:forEach var="item" items="#{cart.items}">
<h:commandButton value="#{item}"/>
</c:forEach>
</h:form>
</body>
</html>
</f:view>
Here the XHTML Page:
这里是 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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Simple JSF</title>
</h:head>
<h:body>
<h:form>
<ui:repeat value="#{cart.items}" var="item">
<h:commandButton value="#{item}" />
</ui:repeat>
</h:form>
</h:body>
</html>
Why do you use a bean in request scope and set you variable there in the session? Keep it simple and change the bean to session scope:
为什么在请求范围内使用 bean 并在会话中设置变量?保持简单并将 bean 更改为会话范围:
@ManagedBean(name = "cart")
@SessionScoped
public class CartBean {
private List<String> items;
public CartBean() {
items = new ArrayList<>();
items.add("shirt");
items.add("skirt");
items.add("trouser");
}
public List<String> getItems() {
return items;
}
}