• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

few obscure things about JSP,EL

 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

look at following code (run on JDK 1.5 but on 1.4 without autoboxing works the same if 55 in scriplet is replaced with "new Integer(55)"):

<%
Map m = new HashMap();
m.put(55, "fifty five");
m.put("a", 55);
request.setAttribute("m", m);
%>

1. ${m[55]}
2. ${m["55"]}
3. ${m[m["a"]]}

When I run this code, I was little amazed with what I saw.

1.
2.
3. fifty five

First two expressions were evaluated to nulls, but third is OK!
It seems that Integer from scriplet is different than Integer from EL.


Another wierd thing.
<%-- Assume that class pl.wcd.Dog is correct javabean class with String age property--%>
<jsp:useBean id="dog" class="pl.wcd.Dog" />

1. <jsp:setProperty name="dog" property="age" value="<%= 55 %>" />
2. <jsp:setProperty name="dog" property="age" value="<%= "55" %>" />
2. <jsp:setProperty name="dog" property="age" value="${55}" />

First throws exception about incopatibile types, but second and third is correctly evaluated. I cannot say why first is wrong.

Can anybody tell why it works as described?
 
Wendy McAvoy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First problem is due to the variables defined in the scriptlet are local. Move map outside of scriptlet and use declaration <%! %> to define the variable.

Second problem is due to the fact that once you use scripting inside a standard action then you lose the automatic type conversion

Hope this helps.

Wendy
 
Wendy McAvoy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First problem is due to the variables defined in the scriptlet are local. Move map outside of scriptlet and use declaration <%! %> to define the variable.

Second problem is due to the fact that once you use scripting inside a standard action then you lose the automatic type conversion

Hope this helps.

Wendy
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a lot simplier than that.
Default numeric types in EL are java.lang.Long and java.lang.Double.

So if I put longs, eveything works fine.
<%
Map m = new HashMap();
m.put(55L, "fifty five");
m.put("a", 55L);
request.setAttribute("m", m);
%>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic