The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

christian.delbianco (1) [Avatar] Offline
#1
Automatic type conversion in <jsp:setProperty>
HI, SOMEONE CAN EXPLAIN ME THIS THING. AT PAGE 277 THE BOOK TELL:

if we use request-time attribute expression in the <jsp:setPropery> action, then no such automatic conversione happens. Consider the following:

<% <br /> String anIntAsString = "30";
String aBoolAsString = "false";
%>

<jsp:setProperty name="user" property="visits" value="<% anIntAsString %>" />
...
...
...
This example will not compile because we have passed a request-time attribute axpression value that evaluate to a String instead of an int. To make it work, we have to explicitly convert the value ecc...

THEN AT PAGE 279 THE BOOK LIST THE SNIPPET CODE:


The following action sets the indexed property permissions using a request time attribute expression:"

<%! char myPerrmission[] = {'A', 'B', 'C'}; %>

<jsp:setProperty name="user" property="permissions" value="<% myPermission %>" />


WHY IN THE FIRST EXAMPLE IS NOT OK AND IN THIS YES ???
THE ONLY DIFFERENCE IS <%! <br /> THANKS
smendez (4) [Avatar] Offline
#2
Re: Automatic type conversion in &lt;jsp:setProperty&gt;
this thing at page 277 is very confusing.
1.-
<% <br /> String anIntAsString = "30";
String aBoolAsString = "false";
%>
anIntAsString and aBoolAsString are Strings!!!
2.-
value="<%= anIntAsString %>"
/ \r
|
here you have passed on a String
the expression <%= anIntAsString %> evaluates to a String
the JSP 2.0 specification:
JSP5.2 <jsp:setProperty> (page 1-105):
...
the usual bean introspection is done to discover what properties
are present, and, foreach, its name, whether it is simple or indexed,
its type, and the setter and getter methods.
...
When assigning from a parameter in the request object, the conversions
described in section JSP.1.12.2.1 are applied, using the target property
to determine the tyrget type.
When assigning from a value givend as a String constant, the conversions
described in section JSP.1.12.2.1 are applied, using the target property
to determine the target type.
==> When assigning from a value given as a request-time attribute,
no type conversion are applied if a scripting expression is used, as
indicated in Section JSP.1.14.2.2 If an EL expression is used, the
type conversion described in section JSP.2.8 are performed. <==

3.- consequence: you have a String, where your Bean is waiting for
an int!!!
quote page 277
to make it work, we have to explicitly convert the value ...
quote page 277
this is a very complicated solution for a trivial problem.
This works fine:
<% <br /> int anInt = 30;
boolean aBool = false;
%>
<jsp:setProperty name="user" property="visits" value="<%= anInt %>" />

<jsp:setProperty name="user" property="valid" value="<%= aBool %>" />


page 279:
for this example, it matters little, if you use a scriplet
<% char[] myPermission = {'A', 'B','B' }; %>
or a declaration
<%! char[] myPermission = {'A', 'B','B' }; %>
(But think about: only request attributes and local variables are thread-safe!)
the example is ok. The clou: the usual bean introspection
( the same error of example page 277:
if the target type is char[] and you pass on an int[], the example
will not compile smilie
sergio