开发者

Avoid grails formatting

开发者 https://www.devze.com 2023-02-09 20:04 出处:网络
Im having an issue with a grails app.开发者_JAVA技巧 In some occassions, when retrieving an instance with MyObject.get(id), I get this exception:

Im having an issue with a grails app.开发者_JAVA技巧

In some occassions, when retrieving an instance with MyObject.get(id), I get this exception:

Expected: class java.lang.Integer, got class java.lang.String

So I did this:

Integer i = Integer.valueOf(params.id);

MyObject.get(i);

However, the a new problem appears. Integer#valueOf(String) seems to return a formatted value, so if params.id is greater than 1000, i gets a decimal point (e.g 1253 ->1.253).

update

After some more research, i've found that the value of params.id is coming with the decimal dot, even though its not present in the query string:

http://somesite.com//action?other=33&id=1485

params.id = 1.485

Whats up with that decimal point? Is there any grailsy approach to this?

Thanks in advance


As described in the Simple Type Converters section of the Grails documentation, you can convert incoming parameters from String to int like so:

def i = params.int('id')


I often use groovy syntax to do this:

def i = (params.id as int)


Use Integer.parseInt instead of Integer.valueOf


I have noted that if I use this:

${fieldValue(bean:myBean,field:'aLongField')}

In a GSP page, it will be formatted and displayed like "1.485" or " 1,485" depending on the current locale.

If I use:

${myBean.aLongField }

or even

${myBean.aLongField.toString() }

Does not call NumberFormat and display the raw value.

0

精彩评论

暂无评论...
验证码 换一张
取 消