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

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

问题描述

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

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

Integer minPrice

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

I then access it in a GSP page as follows:

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

我在 HTML 中得到的是...

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.

这是一个问题,因为我在 HTML FORM 上有一个 SELECT 控件,该控件具有 minPrice 的(非序数)值范围,我想将其作为整数存储在域对象中,但我不想存储某个值数组的索引,我必须在它们之间反复映射,我想要值本身.

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

当我从 SELECT 字段中获取值以回发到服务器时,它正确地具有一个整数值,我坚持使用它.然而,回程从不预先选择下拉列表中的正确行,因为值是这个逗号分隔的字符串.

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.

这在我的代码中的其他地方可以很好地适用于逗号格式不起作用的小数字,并且进出 SELECT 的往返是成功的.但值 >999 不起作用.

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.

文档说该标签将检查作为数据绑定主题的 bean,并从包含在数据绑定期间填充的 bean 的错误对象中的原始提交值或从bean 属性的值.获得该值后,它将自动进行 HTML 编码."

这是我想避免的最后一点,因为它似乎格式化整数.那么,我需要知道什么 Grails/GSP 魔法才能让我的整数作为整数呈现到我的 SELECT 中并预选正确的行?

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?

我根据下面的答案尝试了一些进一步的事情,到目前为止结果非常令人失望......

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

如果我将 <gformatNumber/> 标记放在我的 <g:select/> 中,我会在浏览器中以文本形式获取页面代码.

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

像这样在我的整数值 100000 上使用来自 GSP 的数字格式标签...

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="#" />;

给出 100.请记住,fieldValue 返回 100,000,所以这并不奇怪.

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

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

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.

我想我有一个更广泛的担忧,因为如果 fieldValue 指令的默认(和不可配置)行为大于 999,我似乎无法将整数值作为真正的整数输入我的代码.然而,我无法在 SELECT 控件中预先选择整数值的具体问题并没有消失.此刻我有点不知所措.

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?

推荐答案

是否要显示原始数字?像 100000?

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

可以直接获取字段:

${myBean.minPrice}

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

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