Problem with EL (Expression Language) in JST /JSP

J

jgmaux

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.
 
S

Steve

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

Value:${requestScope.clients[clientnum]}

should do the trick.
 
J

jgmaux

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.

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.
 
L

Lee Crawford

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

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.

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.
 
J

jgmaux

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

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.

(e-mail address removed) 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.
 
L

Lew

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

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

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

- Lew
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top