Grails / Groovy: params URL (max, offset) NumberFormatException Renvoyé lorsque les chaînes vides /

StackOverflow https://stackoverflow.com/questions/4008373

Question

dans le contrôleur

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

si vous entrez dans les urls suivantes

/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)

Y at-il une réponse groovy d'une ligne pour vérifier contre des caractères nuls / chaîne dans la params url?

Était-ce utile?

La solution

Regardez les Notes de version pour Grails 1.2 où ont été introduits nuls convertisseurs de sécurité pour params et attributs de la balise.

Vous devez changer vos lignes ..

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

.. au code suivant:

params.max = Math.min(params.int('max') ?: 10, 20)
params.offset = params.int('offset') ?: 0
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top