<c:foreach>

Hi all,

Here is the code snippet.

<c:forEach var="product" items="${productList.pageList}" varStatus="status">

<tr>

<td>

<b><font color="BLUE"><c:out value="${product.ownerid}"/></font></b>

<img width="100" height="100" src="/springjsp/loadImages.do?id=<c:out value="${product.ownerid}"/>">

</td>

<td>//I need next value of the item here</td>

</tr>

</c:forEach>

Now i want the second item value on the next column of the <tr>.How to do that.I want next value of the item onthe next <td>

Thanx in advance,

Karthik M

[1011 byte] By [KarthikMa] at [2007-11-27 0:28:00]
# 1

> <c:out

> value="${product.ownerid}"/>

Instead of c:out you could simply write it as ${product.ownerid} , you don't need c:out

Similarly here:

> 0" src="/springjsp/loadImages.do?id=<c:out

> value="${product.ownerid}"/>">

write it as :

> 0" src="/springjsp/loadImages.do?id="${product.ownerid}">

> </td>

> <td>//I need next value of the item here</td>

You can nest the loops, use the modulus operator to determine the end of the inner loop.

>

> Now i want the second item value on the next column

> of the <tr>.How to do that.I want next value of the

> item onthe next <td>

appy77a at 2007-7-11 22:28:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

>Instead of c:out you could simply write it as ${product.ownerid} , you don't need c:out

Having said that, the <c:out> tag is still useful for escaping HTML characters in values. I prefer using the <c:out> tag to just the ${expr} in certain cases - particularly when printing string values. Numbers aren't so bad.

Another way of controlling it, is to loop over each item, and only do the <tr> tags on every 'nth' one (as determined by status.count)

<c:forEach var="product" items="${productList.pageList}" varStatus="status">

// print a <tr> tag on even items.

<c:if test="${status % 2 == 0}"><tr></c:if>

<td>

<b><font color="BLUE"><c:out value="${product.ownerid}"/></font></b>

<img width="100" height="100" src="/springjsp/loadImages.do?id=<c:out value="${product.ownerid}"/>">

</td>

// close the <tr> tag on odd items - or on the last item.

<c:if test="${(status %2 == 1) || status.last}"></tr></c:if>

</c:forEach>

evnafetsa at 2007-7-11 22:28:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Thanx for your reply.

When execute the jsp after doing the modifications,Iam getting the exception as follows:

javax.servlet.ServletException: javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "test" with value "${status % 2 == 0}": Attempt to coerce a value of type "javax.servlet.jsp.jstl.core.LoopTagSupport$1$Status" to type "java.lang.Long" (null)

org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)

org.apache.jsp.springjsp.ImageDisplay_jsp._jspService(ImageDisplay_jsp.java:124)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:112)

org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:249)

org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)

org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:827)

org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:736)

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)

org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)

javax.servlet.http.HttpServlet.service(HttpServlet.java:709)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Since iam using JSTL for few days,iam unable to fix.So can you help in that.

Thanks & Regards,

Karthik M

KarthikMa at 2007-7-11 22:28:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> >Instead of c:out you could simply write it as

> ${product.ownerid} , you don't need c:out

> Having said that, the <c:out> tag is still useful for

> escaping HTML characters in values.

c:out inside an attribute value cannot be used in some circumstances e.g: when the JSP is written in XML format, having c:out inside the attribute value breaks the validity/wellformedness of the XML syntax.

In this case use ${fn:escapeXml(someValue)}

the above syntax is also shorter than c:out but requires an additional fn taglib.

appy77a at 2007-7-11 22:28:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You are getting this error because status is an Object of type:

javax.servlet.jsp.jstl.core.LoopTagSupport

> javax.servlet.ServletException:

> javax.servlet.jsp.JspException: An error occurred

> while evaluating custom action attribute "test" with

> value "${status % 2 == 0}": Attempt to coerce a value

> of type

> "javax.servlet.jsp.jstl.core.LoopTagSupport$1$Status"

You need the index property of the varStatus variable, so change it to this:

<c:if test="${status.index %2==0}">

</c:if>

appy77a at 2007-7-11 22:28:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...