Using foreach loop in JSF

Viewed 92679

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.

my bean has:

@ManagedBean(name="MyProfile")
@RequestScoped

And I set my variable myInterestList in my bean with:

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>
1 Answers
Related