Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL tag c:forEach

 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling servlet from a jsp where in servlet construting a collection and setting it at request attribute,then dispatching the request to another view using RequestDispatcher(eg view.jsp).
In view.jsp getting the collection from request attribute and displaying the content using scriptlet is working fine,but when trying to use jstl tag forEach it is giving below error.

org.apache.jasper.JasperException: /view.jsp(52,0) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:376)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:941)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:696)
org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1441)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Validator.validate(Validator.java:1475)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:214)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:461)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:442)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:430)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:274)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.example.web.TestAction.doGet(TestAction.java:59)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

Pls, suggest
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In continuation....
using for:Each in view.jsp as below

<table>
<c:forEach var="test" items="${nums}">
<tr>
<td>
${test}
</td>
</tr>
</c:forEach>
</table>
where nums(key) is the collection set on the request attribute.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if you declare your taglib like

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

You should use JSTL1.1.
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Remko,

I already declare the uri as
<%@ taglib prefix= "c" uri="http://java.sun.com/jstl/core" %>
but letter changed to
<%@ taglib prefix= "c" uri="http://java.sun.com/jstl/core_rt" %>
after that it starts working.
Although i am using Tomcat5 having jsp 2.0 container.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you check the xmlnamespace of your web.xml it should be

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishek Mish:
already declare the uri as

Those are JSTL 1.0 URIs and both are incorrect. You should be using the JSTL 1.1 URIs with Tomcat 5 or later.
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, i changed the uri for JSTL 1.1 as below
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
after that it is working fine..
Pls, correct me if i am wrong.
Rgds,
Abhishek
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
If both the JSTL uri's ie
1.<%@ taglib prefix= "c" uri="http://java.sun.com/jstl/core" %>
2.<%@ taglib prefix= "c" uri="http://java.sun.com/jstl/core_rt" %>
are incorrect and are JSTL 1.0 uri's.
But what wud be the reason when i am using Tomcat 5.0 and JSTL as 1.1, then URI(1) as given above is throwing exception while URI(2) is working fine if both are JSTL 1.0 URI.
Rgds,
Abhishek
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The _rt version of the 1.0 tag set allows run-time expressions to be used. Under JSP 1.x, EL is not evaluated by the container but by the JSTL. Under JSP 2.0, the EL is evaluated by the container and hence is considered a run-time expression.

But the bottom line is that you need to use the right version of the JSTL for the JSP version.
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,
I think i got the point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic