Automatic Type Conversions
The beans used in this chapter have
properties of the Java type String, meaning they
have plain-text values. But as I mentioned in the beginning of this
chapter, a bean property can be of any Java type. As a JSP page
author, you typically don’t have to worry too much
about this, though, since the container can convert text values to
other Java types. It handles the most common types all by itself, but
for more complex types, it needs a little help from the Java
programmer who develops the bean class.
When you use the
<jsp:setProperty> or the JSTL
<c:set> action, the container takes care
automatically of the conversion from text values to the Java types
shown in Table 6-3.
|
Property type |
Conversion method |
|
|
Boolean.valueOf(String) |
|
|
Byte.valueOf(String) |
|
|
String.charAt(0) |
|
|
Double.valueOf(String) |
|
|
Integer.valueOf(String) |
|
|
Float.valueOf(String) |
|
|
Long.valueOf(String) |
|
|
Short.valueOf(String) |
|
|
new String(String) |
For other types, such as a java.util.Date, the JSP
specification defines how a Java programmer can develop a so-called
"property editor” to
handle the conversion. A property editor associated with a bean can
convert a string such as 2002-05-10 to a
Date object that represents this date. How to do
so is described in Chapter 22.
The value returned by ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access