aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSTL tag  c:forEach 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSTL tag  c:forEach" Watch "JSTL tag  c:forEach" New topic
Author

JSTL tag c:forEach

Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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

Joined: Dec 21, 2007
Posts: 29
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

Joined: Dec 28, 2006
Posts: 893
Check if you declare your taglib like

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

You should use JSTL1.1.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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

Joined: Dec 28, 2006
Posts: 893
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

Joined: Jan 10, 2002
Posts: 61054
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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

Joined: Dec 21, 2007
Posts: 29
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

Joined: Jan 10, 2002
Posts: 61054
    
  66

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

Joined: Dec 21, 2007
Posts: 29
Thanks Bear,
I think i got the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL tag c:forEach