Java Reference
In-Depth Information
We get type conversion for action bean properties of these types, includ-
ing nested properties, without having to do anything special. We just
declare the action bean property with its type, and Stripes does the
conversion for us. Let's take a closer look at how these built-in type
converters work.
5.2
Built-in Type Converters
The types for which Stripes does automatic conversion boil down to
these: numbers, dates, booleans, characters, and enumerations. Learn-
ing how these type converters work will help you understand why cer-
tain inputs are successfully converted while others cause a validation
error.
Working with Numbers
For the numerical types— byte , short , int , long , float , double , their wrapper
classes, BigDecimal , and BigInteger —the type converters use a common
strategy for improving their chances of parsing the String input into a
number:
1. Remove the currency symbol, if present. The currency symbol is
the current locale's if available and otherwise the dollar sign ( $ ).
2. If parentheses surround the input, such as (42) , replace them with
a leading negation sign as in -42 .
After performing these operations, the number type converters then use
Java's built-in NumberFormat with the user's locale to attempt parsing
the input into a Number . If parsing fails, a validation error occurs, the
user is sent back to the form, and we can display the error message
with the <s:errors/> tag.
Byte, Short, Integer, Long, BigInteger
The type converters for whole number types accept valid numerical val-
ues within the accepted range of the target type, as shown in Figure 5.2 ,
on the following page. These type converters do not accept decimals in
the input.
Float, Double, BigDecimal
The type converters for the decimal number types follow the same rules
as their whole number cousins, except that they accept decimals. If the
number of decimals exceeds the precision of the target type, rounding is
 
 
 
Search WWH ::




Custom Search