This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL c:forEach help?

 
Mallik Avula
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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"><cut value="${serviceId}"/></td>
</tr>
</c:forEach>
--------------------------------------------------------
where i did mistake
let me know
thanks in advance

ur's
Mallik
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Kamesh Rao
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this becoming about Struts, moved to the Struts forum.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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 ]
 
Alex Kotov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, Brent. Excellent FAQ article! It even works for checkboxes!

-Alex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic