Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > forEach attribute end does not accept expression?

Reply
Thread Tools

forEach attribute end does not accept expression?

 
 
Robert Jones
Guest
Posts: n/a
 
      07-09-2003
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

 
Reply With Quote
 
 
 
 
Wendy S
Guest
Posts: n/a
 
      07-09-2003
"Robert Jones" <(E-Mail Removed)> wrote:
> 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.

--
Wendy in Chandler, AZ


 
Reply With Quote
 
 
 
 
Robert Jones
Guest
Posts: n/a
 
      07-09-2003
Wendy S wrote:
> "Robert Jones" <(E-Mail Removed)> wrote:
>
>>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.
>


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

 
Reply With Quote
 
Wendy S
Guest
Posts: n/a
 
      07-09-2003
"Robert Jones" <(E-Mail Removed)> wrote
> 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.

--
Wendy in Chandler, AZ


 
Reply With Quote
 
Phil Hanna
Guest
Posts: n/a
 
      07-10-2003
On Wed, 09 Jul 2003 19:59:14 GMT, Robert Jones
<(E-Mail Removed)> wrote:

>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" %>
--
Phil Hanna
Author of JSP 2.0: The Complete Reference
http://www.philhanna.com
 
Reply With Quote
 
Robert Jones
Guest
Posts: n/a
 
      07-10-2003
Wendy S wrote:
> "Robert Jones" <(E-Mail Removed)> wrote
>
>>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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<xs:attribute> with ref attribute nested in <xs:attribteGroup> does't work problem Aray XML 0 12-19-2006 09:22 AM
JSP+JSTL cant output values using <c: taglib. "attribute items does not accept any expressions" Anonymous Java 2 08-31-2006 04:13 PM
why does this not work: foreach (my $word) (@word_array){...} TechCrazy Perl Misc 12 06-29-2005 01:08 AM
"'If' must end with a matching 'End If'." <-- BUT IT DOES!! WHY DOESN'TTHE DAMN SERVER SEE IT??? Neo Geshel ASP .Net 2 08-18-2004 01:37 PM
jstl does not work with foreach Quick Function Java 4 07-14-2004 09:42 PM



Advertisments