forEach attribute end does not accept expression?

Discussion in 'Java' started by Robert Jones, Jul 9, 2003.

  1. Robert Jones

    Robert Jones Guest

    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
     
    Robert Jones, Jul 9, 2003
    #1
    1. Advertising

  2. Robert Jones

    Wendy S Guest

    "Robert Jones" <> 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
     
    Wendy S, Jul 9, 2003
    #2
    1. Advertising

  3. Robert Jones

    Robert Jones Guest

    Wendy S wrote:
    > "Robert Jones" <> 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
     
    Robert Jones, Jul 9, 2003
    #3
  4. Robert Jones

    Wendy S Guest

    "Robert Jones" <> 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
     
    Wendy S, Jul 9, 2003
    #4
  5. Robert Jones

    Phil Hanna Guest

    On Wed, 09 Jul 2003 19:59:14 GMT, Robert Jones
    <> 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
     
    Phil Hanna, Jul 10, 2003
    #5
  6. Robert Jones

    Robert Jones Guest

    Wendy S wrote:
    > "Robert Jones" <> 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
     
    Robert Jones, Jul 10, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    2
    Views:
    3,669
    Versteijn
    Aug 18, 2004
  2. Quick Function

    jstl does not work with foreach

    Quick Function, Jul 14, 2004, in forum: Java
    Replies:
    4
    Views:
    805
    Quick Function
    Jul 14, 2004
  3. Anonymous
    Replies:
    2
    Views:
    28,932
    phamlen
    Aug 31, 2006
  4. Gernot Frisch
    Replies:
    6
    Views:
    347
    Gernot Frisch
    Nov 29, 2005
  5. TechCrazy
    Replies:
    12
    Views:
    184
    John Bokma
    Jun 29, 2005
Loading...

Share This Page