Java 在JSF中使用foreach循环

Java 在JSF中使用foreach循环,java,web-services,jsp,jsf,foreach,Java,Web Services,Jsp,Jsf,Foreach,我需要在我的页面上创建一个commandbuttons列表,但是我遇到了一些问题。我通过request.setAttribute从bean中传递列表,当我一次获取一个值时,它似乎起作用,但当我运行foreach循环时,它们似乎都是空的(因此生成,并且有一个默认值0“,”等等,据我所知)。任何帮助都将不胜感激!在添加的代码中,当我在foreach循环之外创建按钮时,我得到了正确的值,但当我运行循环本身时,却没有得到正确的值。列表是整数类型的,以后应该是java对象(遇到相同的问题)。使用JSF版本

我需要在我的页面上创建一个commandbuttons列表,但是我遇到了一些问题。我通过request.setAttribute从bean中传递列表,当我一次获取一个值时,它似乎起作用,但当我运行foreach循环时,它们似乎都是空的(因此生成,并且有一个默认值0“,”等等,据我所知)。任何帮助都将不胜感激!在添加的代码中,当我在foreach循环之外创建按钮时,我得到了正确的值,但当我运行循环本身时,却没有得到正确的值。列表是整数类型的,以后应该是java对象(遇到相同的问题)。使用JSF版本2.2。logtest()和gotoprofile()都会打印出兴趣点

我的豆豆有:

@ManagedBean(name="MyProfile")
@RequestScoped
我在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>
HttpSession session=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
setAttribute(“myInterestProfileName”,profileName);
JSF成功登录





如果您使用的是JSF2,您应该将页面更改为xhtml,然后您可以使用ui:repeat并从facelets中获得更多好处

我制作了两个非常简单的页面,一个是JSP,另一个是XHTML。它们在请求范围中使用托管bean。两者都在工作,并在一行中渲染三个按钮。请注意,我使用Glassfish作为服务器,因为它更容易启动。对于Tomcat(7.x),您可能需要将JSFAPI、jsf impl(2.x)和jstl(1.2)libs复制到类路径中

这是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>

JSP页面
这里是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>

简单JSF
为什么在请求范围中使用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;
  }

}
@ManagedBean(name=“cart”)
@会议范围
公共类CartBean{
私人清单项目;
公共菜豆(){
items=newarraylist();
项目。添加(“衬衫”);
项目。添加(“裙子”);
项目。添加(“裤子”);
}
公共列表getItems(){
退货项目;
}
}

使用${orly}而不是#{orly}我在尝试编译时遇到此错误。“根据TLD,属性值是一个差异值或延迟方法,但指定的值包含一个$-表达式”#{…}模板,不允许在文本体中显示:(如果我使用$,它只显示一个commandbutton,它是空的,尽管myInterestListYay中有4个。你使用哪个版本的JSF?你能发布更多的页面内容吗?我现在用整页更新了我的原始帖子。我将其余的复制到记事本文档中,以确保错误不是由此引起的。我添加了一些文本。)我的托管bean也是如何设置的