You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Claire Suttle <cl...@geoviz.com> on 2003/08/18 22:43:20 UTC

DynaForms - c:forEach wont iterate

Hi,

I've been reading an artice about DynaForms 
(http://developer.com/java/ejb/article.php/2233591) and have been trying to pre-populate the dynaForm from an action class. I've populated an ArrayList, and am now trying to display the contents of that list in a JSP page. 

The problem is that the jsp tag, c:forEach seems to only get the first object out of my ArrayList, and then exits the for loop. If I change the index that the loop is supposed to start at, it will get the object at the correct index, and then exit the loop - again, only getting one object out of the list. Has anyone come across this before, and if so, how did you solve it? I think - famous last words - that my config file is correct, as the ArrayList is found, so I wonder if I'm doing something wrong in the JSP? Any help would be much appreciated.

Struts-config:
<form-bean name="addProjectUserBeanForm" type="org.apache.struts.action.DynaActionForm" scope="session">

<form-property name="users" type="java.util.ArrayList" scope="session" />

</form-bean>



.jsp file:

<c:forEach var="users" items="${addProjectUserBeanForm.map.users}"

    <c:choose>

        <c:when test="${users.added == true}">

            <tr class="contenttext">

                <td width="4%">

                    <img src="/images/ok.gif" alt="Already Added" height="15px" width="15px"/>

                </td>

                <td width="22%">

                    <html:hidden indexed="true" name="users" property="user_Username" write="true" />

                </td>

                <td width="24%">

                    <html:hidden indexed="true" name="users" property="user_Name" write="true" />

                </td>

                <td width="30%">

                    <html:hidden indexed="true" name="users" property="user_EmailAddress" write="true" />

                </td>

                <td width="20%">

                    <html:hidden indexed="true" name="users" property="user_Role" write="true" />

                </td>

        </tr>

    </c:when>

    <c:otherwise>

        <tr class="contenttext">

            <td width="4%">

                <html:checkbox indexed="true" name="users" property="added" />

            </td>

            <td width="22%">

                <html:hidden indexed="true" name="users" property="user_Username" write="true" />

            </td>

            <td width="24%">

                <html:hidden indexed="true" name="users" property="user_Name" write="true" />

            </td>

            <td width="30%">

                <html:hidden indexed="true" name="users" property="user_EmailAddress" write="true" />

            </td>

            <td width="20%">

                <html:select indexed="true" name="users" property="user_Role">

                    <html:option value="Member">Member</html:option>

                < html:option value="Observer">Observer</html:option>

            </html:select>

        </td>

    </tr> 

    </c:otherwise> 

</c:choose>

</c:forEach>


Re: DynaForms - c:forEach wont iterate

Posted by Mark Lowe <ma...@talk21.com>.
You should be able to do the same with jstl you'll just need something 
like

<c:forEach var="user" items="${myForm.map.users}">

even perhaps

<c:forEach var="user" items="${myForm.users}">

As long as the arraylist is being drilled to.


On Monday, August 18, 2003, at 10:46 PM, Claire Suttle wrote:

> Hi,
>
> Changing my jsp to the logic:iterate tags fixed my problem, thanks 
> Mark! I'd
> tried changing to that before, so I was obviously doing something 
> wrong, if
> anyone can work it out, I'd still like to know.
>
> The referring action is as follows:
>
> (The array list population is pretty lengthy - theres a lot of error
> checking and xml parsing, so I've left that out)
> // Populate the user registration form
>
> if (form == null) {
>
>         form = new DynaActionForm();
>
> DynaActionForm bean = (DynaActionForm)form;
>
> bean.set("users", aryList);
>
>
>
> Claire
>
> ----- Original Message -----
> From: "Mark Lowe" <ma...@talk21.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Monday, August 18, 2003 2:13 PM
> Subject: Re: DynaForms - c:forEach wont iterate
>
>
>> You'll want to scope your form to the session when you map it to your
>> action..
>>
>> <action name="myForm" scope="session"..
>>
>> not in form-bean and form-property, struts console is great for the
>> config files.
>>
>> Can i have a look at the referring action (the one thats run before 
>> the
>> form is loaded)?
>>
>> DynaActionForm theForm = (DynaActionForm) form;
>>
>> ArrayList userList = new ArrayList();
>> userList.add(new UserBean());
>> theForm.set("users",userList);
>>
>> In struts speak rather than that nasty jstl stuff (I'm in the minority
>> on this so don't take my work for it) it looks like this...
>>
>> <html:form action="/myaction.do">
>>
>> <logic:iterate id="user" name="myForm" property="users">
>> <html:hidden name="user" property="userName" indexed="true" />
>> </logic:iterate>
>>
>>
>> Cheers Mark
>>
>>
>>
>> On Monday, August 18, 2003, at 09:43 PM, Claire Suttle wrote:
>>
>>> Hi,
>>>
>>> I've been reading an artice about DynaForms
>>> (http://developer.com/java/ejb/article.php/2233591) and have been
>>> trying to pre-populate the dynaForm from an action class. I've
>>> populated an ArrayList, and am now trying to display the contents of
>>> that list in a JSP page.
>>>
>>> The problem is that the jsp tag, c:forEach seems to only get the 
>>> first
>>> object out of my ArrayList, and then exits the for loop. If I change
>>> the index that the loop is supposed to start at, it will get the
>>> object at the correct index, and then exit the loop - again, only
>>> getting one object out of the list. Has anyone come across this
>>> before, and if so, how did you solve it? I think - famous last words 
>>> -
>>> that my config file is correct, as the ArrayList is found, so I 
>>> wonder
>>> if I'm doing something wrong in the JSP? Any help would be much
>>> appreciated.
>>>
>>> Struts-config:
>>> <form-bean name="addProjectUserBeanForm"
>>> type="org.apache.struts.action.DynaActionForm" scope="session">
>>>
>>> <form-property name="users" type="java.util.ArrayList" 
>>> scope="session"
>>> />
>>>
>>> </form-bean>
>>>
>>>
>>>
>>> .jsp file:
>>>
>>> <c:forEach var="users" items="${addProjectUserBeanForm.map.users}"
>>>
>>>     <c:choose>
>>>
>>>         <c:when test="${users.added == true}">
>>>
>>>             <tr class="contenttext">
>>>
>>>                 <td width="4%">
>>>
>>>                     <img src="/images/ok.gif" alt="Already Added"
>>> height="15px" width="15px"/>
>>>
>>>                 </td>
>>>
>>>                 <td width="22%">
>>>
>>>                     <html:hidden indexed="true" name="users"
>>> property="user_Username" write="true" />
>>>
>>>                 </td>
>>>
>>>                 <td width="24%">
>>>
>>>                     <html:hidden indexed="true" name="users"
>>> property="user_Name" write="true" />
>>>
>>>                 </td>
>>>
>>>                 <td width="30%">
>>>
>>>                     <html:hidden indexed="true" name="users"
>>> property="user_EmailAddress" write="true" />
>>>
>>>                 </td>
>>>
>>>                 <td width="20%">
>>>
>>>                     <html:hidden indexed="true" name="users"
>>> property="user_Role" write="true" />
>>>
>>>                 </td>
>>>
>>>         </tr>
>>>
>>>     </c:when>
>>>
>>>     <c:otherwise>
>>>
>>>         <tr class="contenttext">
>>>
>>>             <td width="4%">
>>>
>>>                 <html:checkbox indexed="true" name="users"
>>> property="added" />
>>>
>>>             </td>
>>>
>>>             <td width="22%">
>>>
>>>                 <html:hidden indexed="true" name="users"
>>> property="user_Username" write="true" />
>>>
>>>             </td>
>>>
>>>             <td width="24%">
>>>
>>>                 <html:hidden indexed="true" name="users"
>>> property="user_Name" write="true" />
>>>
>>>             </td>
>>>
>>>             <td width="30%">
>>>
>>>                 <html:hidden indexed="true" name="users"
>>> property="user_EmailAddress" write="true" />
>>>
>>>             </td>
>>>
>>>             <td width="20%">
>>>
>>>                 <html:select indexed="true" name="users"
>>> property="user_Role">
>>>
>>>                     <html:option value="Member">Member</html:option>
>>>
>>>                 < html:option value="Observer">Observer</html:option>
>>>
>>>             </html:select>
>>>
>>>         </td>
>>>
>>>     </tr>
>>>
>>>     </c:otherwise>
>>>
>>> </c:choose>
>>>
>>> </c:forEach>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: DynaForms - c:forEach wont iterate

Posted by Claire Suttle <cl...@geoviz.com>.
Hi,

Changing my jsp to the logic:iterate tags fixed my problem, thanks Mark! I'd
tried changing to that before, so I was obviously doing something wrong, if
anyone can work it out, I'd still like to know.

The referring action is as follows:

(The array list population is pretty lengthy - theres a lot of error
checking and xml parsing, so I've left that out)
// Populate the user registration form

if (form == null) {

        form = new DynaActionForm();

DynaActionForm bean = (DynaActionForm)form;

bean.set("users", aryList);



Claire

----- Original Message -----
From: "Mark Lowe" <ma...@talk21.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, August 18, 2003 2:13 PM
Subject: Re: DynaForms - c:forEach wont iterate


> You'll want to scope your form to the session when you map it to your
> action..
>
> <action name="myForm" scope="session"..
>
> not in form-bean and form-property, struts console is great for the
> config files.
>
> Can i have a look at the referring action (the one thats run before the
> form is loaded)?
>
> DynaActionForm theForm = (DynaActionForm) form;
>
> ArrayList userList = new ArrayList();
> userList.add(new UserBean());
> theForm.set("users",userList);
>
> In struts speak rather than that nasty jstl stuff (I'm in the minority
> on this so don't take my work for it) it looks like this...
>
> <html:form action="/myaction.do">
>
> <logic:iterate id="user" name="myForm" property="users">
> <html:hidden name="user" property="userName" indexed="true" />
> </logic:iterate>
>
>
> Cheers Mark
>
>
>
> On Monday, August 18, 2003, at 09:43 PM, Claire Suttle wrote:
>
> > Hi,
> >
> > I've been reading an artice about DynaForms
> > (http://developer.com/java/ejb/article.php/2233591) and have been
> > trying to pre-populate the dynaForm from an action class. I've
> > populated an ArrayList, and am now trying to display the contents of
> > that list in a JSP page.
> >
> > The problem is that the jsp tag, c:forEach seems to only get the first
> > object out of my ArrayList, and then exits the for loop. If I change
> > the index that the loop is supposed to start at, it will get the
> > object at the correct index, and then exit the loop - again, only
> > getting one object out of the list. Has anyone come across this
> > before, and if so, how did you solve it? I think - famous last words -
> > that my config file is correct, as the ArrayList is found, so I wonder
> > if I'm doing something wrong in the JSP? Any help would be much
> > appreciated.
> >
> > Struts-config:
> > <form-bean name="addProjectUserBeanForm"
> > type="org.apache.struts.action.DynaActionForm" scope="session">
> >
> > <form-property name="users" type="java.util.ArrayList" scope="session"
> > />
> >
> > </form-bean>
> >
> >
> >
> > .jsp file:
> >
> > <c:forEach var="users" items="${addProjectUserBeanForm.map.users}"
> >
> >     <c:choose>
> >
> >         <c:when test="${users.added == true}">
> >
> >             <tr class="contenttext">
> >
> >                 <td width="4%">
> >
> >                     <img src="/images/ok.gif" alt="Already Added"
> > height="15px" width="15px"/>
> >
> >                 </td>
> >
> >                 <td width="22%">
> >
> >                     <html:hidden indexed="true" name="users"
> > property="user_Username" write="true" />
> >
> >                 </td>
> >
> >                 <td width="24%">
> >
> >                     <html:hidden indexed="true" name="users"
> > property="user_Name" write="true" />
> >
> >                 </td>
> >
> >                 <td width="30%">
> >
> >                     <html:hidden indexed="true" name="users"
> > property="user_EmailAddress" write="true" />
> >
> >                 </td>
> >
> >                 <td width="20%">
> >
> >                     <html:hidden indexed="true" name="users"
> > property="user_Role" write="true" />
> >
> >                 </td>
> >
> >         </tr>
> >
> >     </c:when>
> >
> >     <c:otherwise>
> >
> >         <tr class="contenttext">
> >
> >             <td width="4%">
> >
> >                 <html:checkbox indexed="true" name="users"
> > property="added" />
> >
> >             </td>
> >
> >             <td width="22%">
> >
> >                 <html:hidden indexed="true" name="users"
> > property="user_Username" write="true" />
> >
> >             </td>
> >
> >             <td width="24%">
> >
> >                 <html:hidden indexed="true" name="users"
> > property="user_Name" write="true" />
> >
> >             </td>
> >
> >             <td width="30%">
> >
> >                 <html:hidden indexed="true" name="users"
> > property="user_EmailAddress" write="true" />
> >
> >             </td>
> >
> >             <td width="20%">
> >
> >                 <html:select indexed="true" name="users"
> > property="user_Role">
> >
> >                     <html:option value="Member">Member</html:option>
> >
> >                 < html:option value="Observer">Observer</html:option>
> >
> >             </html:select>
> >
> >         </td>
> >
> >     </tr>
> >
> >     </c:otherwise>
> >
> > </c:choose>
> >
> > </c:forEach>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: DynaForms - c:forEach wont iterate

Posted by Mark Lowe <ma...@talk21.com>.
You'll want to scope your form to the session when you map it to your 
action..

<action name="myForm" scope="session"..

not in form-bean and form-property, struts console is great for the 
config files.

Can i have a look at the referring action (the one thats run before the 
form is loaded)?

DynaActionForm theForm = (DynaActionForm) form;

ArrayList userList = new ArrayList();
userList.add(new UserBean());
theForm.set("users",userList);

In struts speak rather than that nasty jstl stuff (I'm in the minority 
on this so don't take my work for it) it looks like this...

<html:form action="/myaction.do">

<logic:iterate id="user" name="myForm" property="users">
	<html:hidden name="user" property="userName" indexed="true" />
</logic:iterate>


Cheers Mark



On Monday, August 18, 2003, at 09:43 PM, Claire Suttle wrote:

> Hi,
>
> I've been reading an artice about DynaForms
> (http://developer.com/java/ejb/article.php/2233591) and have been 
> trying to pre-populate the dynaForm from an action class. I've 
> populated an ArrayList, and am now trying to display the contents of 
> that list in a JSP page.
>
> The problem is that the jsp tag, c:forEach seems to only get the first 
> object out of my ArrayList, and then exits the for loop. If I change 
> the index that the loop is supposed to start at, it will get the 
> object at the correct index, and then exit the loop - again, only 
> getting one object out of the list. Has anyone come across this 
> before, and if so, how did you solve it? I think - famous last words - 
> that my config file is correct, as the ArrayList is found, so I wonder 
> if I'm doing something wrong in the JSP? Any help would be much 
> appreciated.
>
> Struts-config:
> <form-bean name="addProjectUserBeanForm" 
> type="org.apache.struts.action.DynaActionForm" scope="session">
>
> <form-property name="users" type="java.util.ArrayList" scope="session" 
> />
>
> </form-bean>
>
>
>
> .jsp file:
>
> <c:forEach var="users" items="${addProjectUserBeanForm.map.users}"
>
>     <c:choose>
>
>         <c:when test="${users.added == true}">
>
>             <tr class="contenttext">
>
>                 <td width="4%">
>
>                     <img src="/images/ok.gif" alt="Already Added" 
> height="15px" width="15px"/>
>
>                 </td>
>
>                 <td width="22%">
>
>                     <html:hidden indexed="true" name="users" 
> property="user_Username" write="true" />
>
>                 </td>
>
>                 <td width="24%">
>
>                     <html:hidden indexed="true" name="users" 
> property="user_Name" write="true" />
>
>                 </td>
>
>                 <td width="30%">
>
>                     <html:hidden indexed="true" name="users" 
> property="user_EmailAddress" write="true" />
>
>                 </td>
>
>                 <td width="20%">
>
>                     <html:hidden indexed="true" name="users" 
> property="user_Role" write="true" />
>
>                 </td>
>
>         </tr>
>
>     </c:when>
>
>     <c:otherwise>
>
>         <tr class="contenttext">
>
>             <td width="4%">
>
>                 <html:checkbox indexed="true" name="users" 
> property="added" />
>
>             </td>
>
>             <td width="22%">
>
>                 <html:hidden indexed="true" name="users" 
> property="user_Username" write="true" />
>
>             </td>
>
>             <td width="24%">
>
>                 <html:hidden indexed="true" name="users" 
> property="user_Name" write="true" />
>
>             </td>
>
>             <td width="30%">
>
>                 <html:hidden indexed="true" name="users" 
> property="user_EmailAddress" write="true" />
>
>             </td>
>
>             <td width="20%">
>
>                 <html:select indexed="true" name="users" 
> property="user_Role">
>
>                     <html:option value="Member">Member</html:option>
>
>                 < html:option value="Observer">Observer</html:option>
>
>             </html:select>
>
>         </td>
>
>     </tr>
>
>     </c:otherwise>
>
> </c:choose>
>
> </c:forEach>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org