我如何改变GRAILS GSP fieldValue格式整数的方式? [英] How can I change the way GRAILS GSP fieldValue formats Integers?

查看:109
本文介绍了我如何改变GRAILS GSP fieldValue格式整数的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 整数minPrice 
我的域对象中有一个字段,我定义为一个整数

然后我在GSP页面中访问它,如下所示:

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

和我在HTML中得到的是...

  100,000 

它不是整数,它是一个字符串。更糟糕的是它是一个特定语言环境中的格式化字符串。



这是一个问题,因为我在HTML FORM上有一个SELECT控件,它有一个(非序号) minPrice的值,我想以整数的形式存储在我的域对象中,而且我不想将索引存储到一些必须反复映射的值的数组中,我希望值本身。



我的选择控件看起来像这样...

 < g:select name =minPrice
value =$ {fieldValue(bean:personInstance,field:'minPrice')}
onchange =setDirty()
noSelection ='$ {[' 0':'选择数字...']}'
from =$ {[
['name':'100,000','id':100000],
[''名称':'200,000','id':200000],
['name':'300,000','id':300000]
]}
optionKey =idoptionValue =name
/>

当我从SELECT字段获取值回发到服务器时,它正确地有一个Integer值,我坚持。但是,返回行程从不预先选择下拉列表中的右侧行,因为值是用逗号分隔的字符串。



在我的小数字代码的其他地方工作正常其中逗号格式不起作用,并且SELECT和SELECT中的往返成功。但是> 999的值不起作用。

文档称为该标签将检查一个已经成为数据绑定主题的bean并获取该字段既可以来自数据绑定期间bean的错误对象中包含的最初提交的值,也可以来自bean的属性的值。一旦获得该值,它将自动进行HTML编码。



这是我想避免的最后一点,因为它看起来是整数格式。那么,我需要知道什么样的Grails / GSP魔法,这样我就可以将整数呈现为一个整数到我的SELECT中,并预先选择正确的行吗?



编辑:
我根据下面的答案尝试了一些更进一步的东西,迄今为止结果令人失望......



如果我将< gformatNumber /> 标记放入我的< g:select /> I中

 < g:select name =minPrice
value = '< g:formatNumber number =$ {fieldValue(bean:personInstance,field:'minPrice')}format =#/>'
onchange =setDirty()
noSelection ='$ {['0':'Select a number ...']}'
from =$ {[
['name':'100,000','id':100000],
['name':'200,000','id':200000],
['name':'300,000','id':300000],
]}
optionKey =idoptionValue =name
/>

使用GSP中的数字格式标记,我的Integer值为100000,像这样...

  var x =< g:formatNumber number =$ {fieldValue(bean:personInstance,field:'minPrice')}format = #/> ;; 

给出 100 。请记住, fieldValue 会返回 100,000 ,所以这并不意外。



如果我使用这样的jsp taglib ...

 <%@ taglib prefix =fmt uri =http://java.sun.com/jsp/jstl/fmt%> 
var y =< fmt:formatNumber value =$ {fieldValue(bean:personInstance,field:'minPrice')}pattern =。00/> ;;

我从页面编译器得到一个错误无法将给定对象格式化为数字



我想我有一个更广泛的关注点,我似乎无法将Integer值作为真正的整数存入我的代码中由于fieldValue指令的默认(和不可配置)行为大于999。然而,我不能在SELECT控件中预先选择一个Integer值的具体问题不会消失。目前我有点亏损。

任何人有任何进一步的想法?

解决方案

你想显示原始数字吗?像100000?

您可以直接获得该字段:

  $ {myBean.minPrice} 


I have a field in my domain object which I define as an Integer...

Integer minPrice

I then access it in a GSP page as follows:

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

and what I get in my HTML is...

100,000

which is not an Integer, it's a String. Worse still it's a formatted String in a particular locale.

This is a problem because I have a SELECT control on an HTML FORM which has a (non-ordinal) range of values for minPrice which I want to store in my domain object as integers, and I don't want to store an index to some array of values that I have to repeatedly map back and forth between, I want the value itself.

My select control looks like this...

<g:select name="minPrice" 
value="${fieldValue(bean: personInstance, field: 'minPrice')}"  
onchange="setDirty()"
noSelection='${['0':'Select a number...']}'
from="${[
    ['name':'100,000', 'id':100000],
    ['name':'200,000', 'id':200000],
    ['name':'300,000', 'id':300000]
    ]}"
optionKey="id" optionValue="name"
/>

When I get the value from the SELECT field to post back to the server it correctly has an Integer value, which I persist. However the return trip never pre-selects the right row in the drop-down because the value is this comma separated String.

This works fine elsewhere in my code for small numbers where the comma formatting doesn't come into play, and the round-trip in and out of the SELECT is successful. But values >999 don't work.

The docs say "This tag will inspect a bean which has been the subject of data binding and obtain the value of the field either from the originally submitted value contained within the bean's errors object populating during data binding or from the value of a bean's property. Once the value is obtained it will be automatically HTML encoded."

It's that last bit that I want to avoid as it appears to format Integers. So, what little bit of Grails/GSP magic do I need to know so I can get my Integer to be rendered as an integer into my SELECT and pre-select the right row?

EDIT: I have tried some further things based on the answers below, with pretty disappointing results so far...

If I put the <gformatNumber/> tag in my <g:select/> I get the page code as text in the browser.

<g:select name="minPrice" 
value='<g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}" format="#" />'
onchange="setDirty()"
noSelection='${['0':'Select a number...']}'
from="${[
['name':'100,000', 'id':100000],
['name':'200,000', 'id':200000],
['name':'300,000', 'id':300000],
]}"
optionKey="id" optionValue="name"
/>

Using the number format tag from GSP on my Integer value of 100000 like this...

var x = <g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}" format="#" />;

gives 100. Remember that the fieldValue gives back 100,000, so this is not a surprise.

If I use the jsp taglib like this...

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
var y = <fmt:formatNumber value="${fieldValue(bean: personInstance, field: 'minPrice')}" pattern=".00"/>;

I get an error from the page compiler Cannot format given Object as a Number.

I guess I have a wider concern than I can't seem to get an Integer value as a genuine integer into my code if it is greater than 999 because of the default (and unconfigurable) behaviour of the fieldValue directive. However my specific problem of not being able to pre-select an Integer value in a SELECT control is not going away. At the moment I'm at a bit of a loss.

Anyone have any further ideas?

解决方案

Do you want to show the raw number? like 100000?

You can get the field directly:

${myBean.minPrice}

这篇关于我如何改变GRAILS GSP fieldValue格式整数的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆