forEach attribute end does not accept expression?

R

Robert Jones

Folks,

I'm trying to use the forEach tag from the JSTL in a small JSP page. I
want to set the end attribute with an EL expression, but Tomcat is
repsonding with the following error:

org.apache.jasper.JasperException: /listloc.jsp(15,2) According to TLD
or attribute directive in tag file, attribute end does not accept any
expressions

I'm under the impression that the end attribute can be set dynamically,
which means it should accept an expression. However it isn't and I have
no idea why. Google searches and searches of the Java forums at Sun
have proven fruitless. Does anyone have any ideas?

Anyway, here is the offending code.

<jsp:useBean id="mybean" scope="session"
class="path.to.classfile" />

<c:forEach begin="0" end="${mybean.length}" step="1" var="i">
<tr>
<td><salsys:location index="${i}" type="type1" /></td>
<td><salsys:location index="${i}" type="type2" /></td>
<td><a href="./modify?">Modify</td>
<td><a href="./delete?">Delete</td>
<td><a href="./review?">Review</td>
<td><a href="./failed?">Failed</td>
<td><a href="./test?">Test</td>
<td><a href="./setup?">Setup</td>
</tr>
</c:forEach>

mybean.length does have a value, and it is correct.

I'm using Tomcat 5.0.2 with J2SDK1.4.2.

Thanks,
Rob
 
W

Wendy S

Robert Jones said:
I'm trying to use the forEach tag from the JSTL in a small JSP page. I
want to set the end attribute with an EL expression,
I'm under the impression that the end attribute can be set dynamically,

This is the JSTL reference I use most:
http://www.manning.com/bayern/appendixA.pdf

You should also read the JSTL specification. None of the examples I've seen
use an expression for 'end' and the error message is pretty clear. I don't
think this is allowed.

However, the 'varStatus' attribute will allow you to assign the status of
the loop to a variable. I'm almost certain that that will give you the
counter, you may have to add 1 depending on what you want.
 
R

Robert Jones

Wendy said:
This is the JSTL reference I use most:
http://www.manning.com/bayern/appendixA.pdf

You should also read the JSTL specification. None of the examples I've seen
use an expression for 'end' and the error message is pretty clear. I don't
think this is allowed.

However, the 'varStatus' attribute will allow you to assign the status of
the loop to a variable. I'm almost certain that that will give you the
counter, you may have to add 1 depending on what you want.

Yes, the error message is quite clear, however, I don't think that's how
forEach is supposed to be implemented. I looked at the c.tld file in
the standard.jar file and found the forEach section. Sure enough, it's
not dynamic.

<attribute>
<name>end</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>

So, I just changed the rtexprvalue to 'true' and it works. Now, I don't
know if this is valid or correct as I have not read the spec, but I've
come across several references that indicate it should be dynamically
setable, i.e. JavaServer Pages (O'Reilly, p519).

At any rate, thanks for the help and reference. I'll probably go
straight to the source on this one. It would be pretty braindead for
this not to be dynamically setable. What programmer knows how many
times he is going to need to loop?

Robert Jones
 
W

Wendy S

Robert Jones said:
Yes, the error message is quite clear, however, I don't think that's how
forEach is supposed to be implemented.

The specification says:
6.2 <c:forEach> Repeats its nested body content over a collection of
objects, or repeats it a fixed number of times.

So begin/end is for a fixed number of times. Strange but true. I've never
used it.

What's the "myBean" class like? Is it itself a Collection, or does it have
a method that returns one?

Assuming "myBean" is itself a collection, this might work:

<c:forEach items="${myBean}" varStatus="i">
<tr><td><salsys:location index="${i}" type="type1" /></td></tr>
</c:forEach>

I realize you have a workaround, but I would be hesitant to depend on
behavior that violates the spec, you might find that it disappears in a
later release. Also, didn't you say you're using Tomcat 5? Is there
anything in the JSP 2.0 spec that might help, I thought that a lot of JSTL
became part of JSP and you didn't have to use the <c:> tags anymore.
 
P

Phil Hanna

Folks,

I'm trying to use the forEach tag from the JSTL in a small JSP page. I
want to set the end attribute with an EL expression, but Tomcat is
repsonding with the following error:

org.apache.jasper.JasperException: /listloc.jsp(15,2) According to TLD
or attribute directive in tag file, attribute end does not accept any
expressions

I'm under the impression that the end attribute can be set dynamically,
which means it should accept an expression. However it isn't and I have
no idea why. Google searches and searches of the Java forums at Sun
have proven fruitless. Does anyone have any ideas?

Your code is fine. Just change your taglib directive from this:

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

to this:

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

Robert Jones

Wendy said:
The specification says:
6.2 <c:forEach> Repeats its nested body content over a collection of
objects, or repeats it a fixed number of times.

So begin/end is for a fixed number of times. Strange but true. I've never
used it.

Yes, that makes sense, and I think it is consistent with what I am
trying to do. The value is only dynamic in the sense that it can be
different between instantiations of the web page, meaning that is
essentially fixed when the page is loaded. I just want to assign to the
end attribute based on and EL expression.

What's the "myBean" class like? Is it itself a Collection, or does it have
a method that returns one?

Assuming "myBean" is itself a collection, this might work:

<c:forEach items="${myBean}" varStatus="i">
<tr><td><salsys:location index="${i}" type="type1" /></td></tr>
</c:forEach>

The myBean class is not itself a Collection. I'll look into that to see
if it is applicable. I know very little about Collections. Thanks for
the suggestion.
I realize you have a workaround, but I would be hesitant to depend on
behavior that violates the spec, you might find that it disappears in a
later release. Also, didn't you say you're using Tomcat 5? Is there
anything in the JSP 2.0 spec that might help, I thought that a lot of JSTL
became part of JSP and you didn't have to use the <c:> tags anymore.

Of course this workaround is a bad idea. I am certainly not advocating
it, I'm just noting that it works. I am using Tomcat 5. I haven't gone
through the JSP 2.0 spec yet. The main reason I am using Tomcat 5 is
because the JSP 2.0 spec makes using the EL in custom tags transparent.

As a side note, but probably relevent, I noticed that the 'value'
attribute of 'out' responds the same way. *Certainly* output to a
browser can be dynamic?

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top