jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes JSTL c:forEach help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSTL c:forEach help?" Watch "JSTL c:forEach help?" New topic
Author

JSTL c:forEach help?

Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
Hi
i have to iterate over ArrayList with c:forEach but it is not working
this is my Action class code
----------------------------------------
query = "SELECT SERVICEID, SERVICENAME, DESCRIPTION FROM SERVICES ORDER BY SERVICEID";
rs = stmt.executeQuery(query);

while(rs.next())
{
rsf = new RoleServicesViewForm();
rsf.setServiceId(rs.getString("SERVICEID"));
rsf.setServiceName(rs.getString("SERVICENAME"));
rsf.setServiceDesc(rs.getString("DESCRIPTION"));
services.add(rsf);
}

request.setAttribute("SERVICES",services);

this is my jsp code
------------------------------------
<c:forEach items="${SERVICES}" var="serviceId" >
<tr>
<td class="rightsubheading1" colspan="2"></td>
<td class="rightsubheading1" colspan="2"><cut value="${serviceId}"/></td>
</tr>
</c:forEach>
--------------------------------------------------------
where i did mistake
let me know
thanks in advance

ur's
Mallik


Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Originally posted by Mallik Avula:

ur's


Firstly. "ur" is not a word. Please use real words such as "your" when posting.

Originally posted by Mallik Avula:
but it is not working


Secondly, these are the four most useless words on earth. How is it not working.

Originally posted by Mallik Avula:

request.setAttribute("SERVICES",services);


As a convention, scoped variables follow the same naming conventions as Java variables, so naming the scoped variable "services" will make your code easier to follow for all.

Finally, how are you traversing from the servlet to the JSP? That's the most important aspect of your code and you left it out.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
Thanks for your suggestion
i am very new to this group
next, the context is:

i am using struts for my project.just because i have some comparisons that's why i go for JSTL.
i am retrieving values from database and putting each row values in a FormBean then adding that form bean to ArrayList. Finally i place this ArrayList in request object.
i want to iterate over this ArrayList and display each row in a table row

now let me know how can i do this.

warm regards
Mallik
Kamesh Rao
Ranch Hand

Joined: Dec 24, 2006
Posts: 35
Hi Mallik

Lets make it simple

-->Retrieve all the values from the db(as you already done)
-->Set the list to the formbean
-->In the jsp iterate the list as follows

<c:forEach items="${formBeanName.listPropertyName}" var="serviceId">
</c:forEach>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Since this becoming about Struts, moved to the Struts forum.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
<c:forEach items="${SERVICES}" var="serviceId" >
<tr>
<td class="rightsubheading1" colspan="2"></td>
<td class="rightsubheading1" colspan="2"><cut value="${serviceId}"/></td>
</tr>
</c:forEach>


The problem with the above code is that in this case "serviceId" represents the entire JavaBean. When you try to display it with the <cut> tag, you're getting the result of the toString() method, which if you haven't overridden it, shows you the Java object Id, which doesn't look pretty and doesn't mean a whole lot in this context. You can display the properties of the bean by using dot notation. Here's a modified version of the above that should work:


[ December 27, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Alex Kotov
Greenhorn

Joined: Sep 26, 2007
Posts: 2
This is all fine. But if I need to modify the ? How would I do it in Struts?

Thanks,
Alex
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
If you need to provide a text box where the user's can edit the description field, then you need to look into indexed properties. You can search this forum or check the entry in this forum's FAQ section.

- Brent
Alex Kotov
Greenhorn

Joined: Sep 26, 2007
Posts: 2
Thanks, Brent. Excellent FAQ article! It even works for checkboxes!

-Alex
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL c:forEach help?