jstl foreach loop attribute items does not accept any expressions'

Hi,

I'm trying to use c:foreach loop to loopthrough a list from request object.

<% List idList = ( List ) request.getAttribute("idList" ) ; %>

<c:forEach var="id" items="<%=idList %>">

<TR>

<TD>==<c:out value="${id}" />==</TD>

</TR>

</c:forEach>

But I got error msg saying:

According to TLD or attribute directive in tag file, attribute items does not accept any expressions'

Any ideas of how to fix this?

Thank you.

cc

[557 byte] By [java_cca] at [2007-10-2 5:16:47]
# 1

If you are using JSTL, you should also be using the EL syntax ${expr} rather than <%= expr %>

<c:forEach var="id" items="${requestScope.idList}">

<tr><td>==<c:out value="${id}"/>==</td></tr>

</c:forEach>

This error message will only show up with JSTL1.0 as it the standard library only accepts ${expr} and not <%= expr %>

If you have a JSP2.0 container (eg Tomcat 5) then you should be using JSTL1.1 which DOES accept runtime expressions.

With Tomcat 5 you should use the uri:

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

and make sure your web.xml file declares itself as version 2.4

In any case you should still be using ${expr} rather than <%= expr %> with JSTL tags.

Cheers,

evnafets

evnafetsa at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

i am facing the same problem. Same error is occuring on my system

However I have checked my web.xml file, the version is declared as "2.4" and I am purely using expression.

My file is very simple

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

<html>

<body>

<c:catch>

inside catch

<% int x=4/2; %>

<c:out value="${x}"/>

</c:catch>

jghj

</body>

</html>

i am getting the output as

org.apache.jasper.JasperException: /count.jsp(11,4) According to TLD or attribute directive in tag file, attribute value does not accept any expressions

org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)

org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)

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.Validator$ValidateVisitor.visit(Validator.java:716)

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:495)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:476)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:464)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)

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

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:802)

Please let me know how to fix this

Thanks deepti

DeeptiChhabraa at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I had the same problem.. doing this solved it..

change your taglib reference from

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

to

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

- Rakesh

Rakesh.TAa at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

>change your taglib reference from

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

>to

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

This solution, while it works, is not preferred.

You should use the correct JSTL1.1 URI with a JSP2.0 container:

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

The "rt" versions of the JSTL libraries should only be used if

- you have a JSP1.2 container (eg Tomcat 4)

- you want to use runtime expressions like <%= expr %> with the JSTL tags.

Otherwise you should be using the regular taglib.

evnafetsa at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Many thanks for that. It solved the problem.

One small question though. I am running NetBeans 3.6(I know its old ;-)) which has Tomcat 5-so why do I still need to use the core_rt fix?Your reply seems to suggest that I only need this fix if I have Tomcat 4?

All the best

Adrian

Message was edited by: Adrian

ariadne

ariadnea at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

>which has Tomcat 5-so why do I still need to use the core_rt fix?

>Your reply seems to suggest that I only need this fix if I have Tomcat 4?

As I said in my earlier reply you should NOT be using core_rt with Tomcat 5

Yes it works, but it is not optimal.

You should be using JSTL1.1 with Tomcat 5.

You should be using the JSTL1.1 URIs to import your tag libraries.

Use the URI I posted previously.

evnafetsa at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

This is a nice short format:

> <c:forEach var="id"

> items="${requestScope.idList}">

> <tr><td>==<c:out value="${id}"/>==</td></tr>

> </c:forEach>

It could also be writen as

<jsp:useBean id="idList" type="java.util.Arraylist" scope="request"/>

<c:forEach var="id" items=${idList}">

<tr><td>==<c:out value="${id}"/>==</td></tr>

</c:forEach>

The above allows you to use the idList variable from the memory rather than from the request object if you were to loop over the same ArrayList twice on the same JSP page.

Just my 2 cents.

appy77a at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks it worked for me.
bhupenpatia at 2007-7-16 1:19:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...