Type Conversion Methods
If you prefer to avoid the overhead of
Data Binding and simply want to convert incoming parameters (typically Strings) into another more appropriate type the
params object has a number of convenience methods for each type:
def total = params.int('total')
The above example uses the
int
method, there are also methods for
boolean
,
long
,
char
,
short
and so on. Each of these methods are null safe and safe from any parsing errors so you don't have to perform any addition checks on the parameters.
These same type conversion methods are also available on the tagLibraries parameter of GSP tags.
Handling Multi Parameters
A common use case is dealing with multiple request parameters of the same name. For example you could get a query string such as
?name=Bob&name=Judy
.
In this case dealing with 1 parameter and dealing with many has different semantics since Groovy's iteration mechanics for
String
iterate over each character. To avoid this problem the
params object provides a
list
method that always returns a list:
for(name in params.list('name')) {
println name
}