views:

30

answers:

1

in the controller

 params.max = Math.min(params?.max?.toInteger() ?: 10, 20)
 params.offset = params?.offset?.toInteger() ?: 0

if you enter in the following urls

/books?offset=10&max=              //error
/books?offset=10&max=sdf          //error
/books?offset=&max=10            //works
/books?offset=adsfa&max=10      //error


java.lang.NumberFormatException: For input string: "asdf"

        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

        at java.lang.Integer.parseInt(Integer.java:449)

        at java.lang.Integer.valueOf(Integer.java:554)

Is there a one line groovy answer to check against null/string characters in the url params?

+3  A: 

Have a look at the Release Notes for Grails 1.2 where null safe converters for params and tag attributes were introduced.

You should change your lines..

params.max = Math.min(params?.max?.toInteger() ?: 10, 20)
params.offset = params?.offset?.toInteger() ?: 0

..to the following code:

params.max = Math.min(params.int('max') ?: 10, 20)
params.offset = params.int('offset') ?: 0
codescape