Problem with EL (Expression Language) in JST /JSP

Discussion in 'Java' started by jgmaux@telefonica.net, Dec 29, 2006.

  1. Guest

    Hi,

    I have a problem with JSTL EL (Expression Language) in JSP.

    The follow example show my problem.:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <body>
    <%
    java.util.HashMap clients = new java.util.HashMap();
    clients.put("1","John");
    clients.put("2","Peter");
    clients.put("3","Gerald");
    clients.put("4","Nick");
    request.setAttribute("clients",clients);
    %>
    <c:forEach var='clientnum' begin='1' end='4'>
    <br>
    Client Nº::${clientnum}
    </br>
    <br>
    Value:${requestScope.clients["${clientnum}"]}
    </br>
    </c:forEach>
    </body>
    </html>

    My code don't display the "clients" values...
    ¿Where is the problem?
    I don't want to use iteration, please....
    Thanks in advance.
     
    , Dec 29, 2006
    #1
    1. Advertising

  2. Steve Guest

    The problem is with the nested expressions, which you shouldn't need. I
    think that

    Value:${requestScope.clients[clientnum]}

    should do the trick.

    wrote:
    > Hi,
    >
    > I have a problem with JSTL EL (Expression Language) in JSP.
    >
    > The follow example show my problem.:
    >
    > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <html>
    > <body>
    > <%
    > java.util.HashMap clients = new java.util.HashMap();
    > clients.put("1","John");
    > clients.put("2","Peter");
    > clients.put("3","Gerald");
    > clients.put("4","Nick");
    > request.setAttribute("clients",clients);
    > %>
    > <c:forEach var='clientnum' begin='1' end='4'>
    > <br>
    > Client Nº::${clientnum}
    > </br>
    > <br>
    > Value:${requestScope.clients["${clientnum}"]}
    > </br>
    > </c:forEach>
    > </body>
    > </html>
    >
    > My code don't display the "clients" values...
    > ¿Where is the problem?
    > I don't want to use iteration, please....
    > Thanks in advance.
    >
     
    Steve, Dec 29, 2006
    #2
    1. Advertising

  3. Guest

    Thanks, Steve.

    I try your solution, but I have same problem.....



    Steve ha escrito:

    > The problem is with the nested expressions, which you shouldn't need. I
    > think that
    >
    > Value:${requestScope.clients[clientnum]}
    >
    > should do the trick.
    >
    > wrote:
    > > Hi,
    > >
    > > I have a problem with JSTL EL (Expression Language) in JSP.
    > >
    > > The follow example show my problem.:
    > >
    > > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > > <html>
    > > <body>
    > > <%
    > > java.util.HashMap clients = new java.util.HashMap();
    > > clients.put("1","John");
    > > clients.put("2","Peter");
    > > clients.put("3","Gerald");
    > > clients.put("4","Nick");
    > > request.setAttribute("clients",clients);
    > > %>
    > > <c:forEach var='clientnum' begin='1' end='4'>
    > > <br>
    > > Client Nº::${clientnum}
    > > </br>
    > > <br>
    > > Value:${requestScope.clients["${clientnum}"]}
    > > </br>
    > > </c:forEach>
    > > </body>
    > > </html>
    > >
    > > My code don't display the "clients" values...
    > > ¿Where is the problem?
    > > I don't want to use iteration, please....
    > > Thanks in advance.
    > >
     
    , Dec 29, 2006
    #3
  4. Lee Crawford Guest

    I think the problem is that the forEach tag is generating a numeric
    type for the clientnum variable and the ${clients[clientnum]} syntax is
    failing to interpret the expression correctly because it can't use an
    int to index a map and isn't making the leap to try and coerce the
    value to a string. If you convert the clientnum to a string explicitly
    it will work:

    Add this at the top:

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

    and use:

    Value: ${clients[fn:toLowerCase (clientnum)]}

    alternately, if the only information being carrier in the map keys is
    an integer perhaps a List would do?

    <%
    final List clients = new ArrayList ();
    clients.add ("John");
    clients.add ("Peter");
    clients.add ("Gerald");
    request.setAttribute ("clients", clients);
    %>

    <c:forEach var="client" varStatus="status" items="${clients}">
    ${status.count}: '${client}' <br/>
    </c:forEach>

    --lee

    wrote:
    > Thanks, Steve.
    >
    > I try your solution, but I have same problem.....
    >
    >
    >
    > Steve ha escrito:
    >
    > > The problem is with the nested expressions, which you shouldn't need. I
    > > think that
    > >
    > > Value:${requestScope.clients[clientnum]}
    > >
    > > should do the trick.
    > >
    > > wrote:
    > > > Hi,
    > > >
    > > > I have a problem with JSTL EL (Expression Language) in JSP.
    > > >
    > > > The follow example show my problem.:
    > > >
    > > > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > > > <html>
    > > > <body>
    > > > <%
    > > > java.util.HashMap clients = new java.util.HashMap();
    > > > clients.put("1","John");
    > > > clients.put("2","Peter");
    > > > clients.put("3","Gerald");
    > > > clients.put("4","Nick");
    > > > request.setAttribute("clients",clients);
    > > > %>
    > > > <c:forEach var='clientnum' begin='1' end='4'>
    > > > <br>
    > > > Client Nº::${clientnum}
    > > > </br>
    > > > <br>
    > > > Value:${requestScope.clients["${clientnum}"]}
    > > > </br>
    > > > </c:forEach>
    > > > </body>
    > > > </html>
    > > >
    > > > My code don't display the "clients" values...
    > > > ¿Where is the problem?
    > > > I don't want to use iteration, please....
    > > > Thanks in advance.
    > > >
     
    Lee Crawford, Dec 30, 2006
    #4
  5. Lew Guest

    wrote:
    >>> <br>
    >>> Client Nº::${clientnum}
    >>> </br>
    >>> <br>
    >>> Value:${requestScope.clients["${clientnum}"]}
    >>> </br>


    This is not a correct use of the <br> tag. It does not allow content.

    In other words, you do not surround text with a <br>. Your best bet is to use
    the short-form closed idiom: <br/>.

    - Lew
     
    Lew, Dec 30, 2006
    #5
  6. Guest

    Thanks Lee,

    I try your solution, and it work's .

    Thanks!!!!!


    Lee Crawford ha escrito:

    > I think the problem is that the forEach tag is generating a numeric
    > type for the clientnum variable and the ${clients[clientnum]} syntax is
    > failing to interpret the expression correctly because it can't use an
    > int to index a map and isn't making the leap to try and coerce the
    > value to a string. If you convert the clientnum to a string explicitly
    > it will work:
    >
    > Add this at the top:
    >
    > <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"
    > %>
    >
    > and use:
    >
    > Value: ${clients[fn:toLowerCase (clientnum)]}
    >
    > alternately, if the only information being carrier in the map keys is
    > an integer perhaps a List would do?
    >
    > <%
    > final List clients = new ArrayList ();
    > clients.add ("John");
    > clients.add ("Peter");
    > clients.add ("Gerald");
    > request.setAttribute ("clients", clients);
    > %>
    >
    > <c:forEach var="client" varStatus="status" items="${clients}">
    > ${status.count}: '${client}' <br/>
    > </c:forEach>
    >
    > --lee
    >
    > wrote:
    > > Thanks, Steve.
    > >
    > > I try your solution, but I have same problem.....
    > >
    > >
    > >
    > > Steve ha escrito:
    > >
    > > > The problem is with the nested expressions, which you shouldn't need. I
    > > > think that
    > > >
    > > > Value:${requestScope.clients[clientnum]}
    > > >
    > > > should do the trick.
    > > >
    > > > wrote:
    > > > > Hi,
    > > > >
    > > > > I have a problem with JSTL EL (Expression Language) in JSP.
    > > > >
    > > > > The follow example show my problem.:
    > > > >
    > > > > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    > > > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > > > > <html>
    > > > > <body>
    > > > > <%
    > > > > java.util.HashMap clients = new java.util.HashMap();
    > > > > clients.put("1","John");
    > > > > clients.put("2","Peter");
    > > > > clients.put("3","Gerald");
    > > > > clients.put("4","Nick");
    > > > > request.setAttribute("clients",clients);
    > > > > %>
    > > > > <c:forEach var='clientnum' begin='1' end='4'>
    > > > > <br>
    > > > > Client Nº::${clientnum}
    > > > > </br>
    > > > > <br>
    > > > > Value:${requestScope.clients["${clientnum}"]}
    > > > > </br>
    > > > > </c:forEach>
    > > > > </body>
    > > > > </html>
    > > > >
    > > > > My code don't display the "clients" values...
    > > > > ¿Where is the problem?
    > > > > I don't want to use iteration, please....
    > > > > Thanks in advance.
    > > > >
     
    , Dec 30, 2006
    #6
  7. Lew Guest

    Please don't top-post. (Post re-ordered for clarity.)

    Lee Crawford ha escrito:
    >> <c:forEach var="client" varStatus="status" items="${clients}">
    >> ${status.count}: '${client}' <br/>
    >> </c:forEach>


    wrote:
    > Thanks Lee,
    >
    > I try your solution, and it work's [sic].
    >
    > Thanks!!!!!


    Notice that their solution uses the short-form closed "<br/>" tag.

    - Lew
     
    Lew, Dec 30, 2006
    #7
    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. vr
    Replies:
    6
    Views:
    675
    Chris Smith
    Jan 23, 2004
  2. Jeff Taylor

    JSP include and expression language

    Jeff Taylor, Apr 6, 2004, in forum: Java
    Replies:
    0
    Views:
    581
    Jeff Taylor
    Apr 6, 2004
  3. Replies:
    1
    Views:
    940
    Chris Smith
    Jun 9, 2005
  4. David

    Expression Language in JSP

    David, Sep 13, 2005, in forum: Java
    Replies:
    1
    Views:
    2,742
    David
    Sep 26, 2005
  5. Edward
    Replies:
    1
    Views:
    5,867
    ge0rge
    Apr 3, 2006
Loading...

Share This Page