Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

JSTL c:forEach help?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"><c:out value="${serviceId}"/></td>
</tr>
</c:forEach>
--------------------------------------------------------
where i did mistake
let me know
thanks in advance

ur's
Mallik
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mallik Avula
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this becoming about Struts, moved to the Struts forum.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<c:forEach items="${SERVICES}" var="serviceId" >
<tr>
<td class="rightsubheading1" colspan="2"></td>
<td class="rightsubheading1" colspan="2"><c:out 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 <c:out> 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 ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is all fine. But if I need to modify the ? How would I do it in Struts?

Thanks,
Alex
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Brent. Excellent FAQ article! It even works for checkboxes!

-Alex
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic