Grails 数据绑定与十进制分隔符 [英] Grails databinding with decimal delimiter

查看:21
本文介绍了Grails 数据绑定与十进制分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Double 表单字段上使用带有十进制分隔符的 grail 自动数据绑定ex: Test t = new Test(params)"时遇到问题.经过几次搜索,我发现它与浏览器语言环境有关.

I have a problem when I use grail automatic databinding "ex: Test t = new Test(params)" with decimal delimiter on Double form field. After few search, I've found that it is relative to the browser Locale.

示例:如果我输入小数点后用点3.45"分隔的数字,则它不起作用(数字的小数部分被忽略).它在我的数据库中存储 3.0

Example : If I enter number with decimals separated by a dot '3.45' it does not work (The decimal part of the number is ignored). It store 3.0 in my database

如果我进行相同的测试,但使用逗号3,45"作为小数点分隔符,则一切正常.Grails 在数据库中存储 3.45.

If I do the same test but using a comma '3,45' for decimal separator, everything works fine. Grails store 3.45 in the database.

问题是没有人用逗号分隔符输入数字.(设置为 fr_CA 时,即使是小键盘也会输入一个点.")

The problem is that nobody enter number with comma delimiter. (even the numpad enter a dot '.' when set to fr_CA)

我已经找到了一些解决方案,比如注册自定义数字编辑器(当你有很多应用程序时有点痛苦)或将全局 localeResolver 设置为 en_US(最后一个不起作用,因为我失去了我的所有国际化功能应用).

I've already found some solutions like register custom number editor (bit painful when you have many apps) or set the global localeResolver to en_US (that last one doesn't do the job because I loose all the internationalization functionality of my app).

那么,有人有解决这个问题的简单方法吗?

So, does someone have an easy solution to fix that problem?

使用:圣杯:2.2.0浏览器:谷歌浏览器(语言环境 fr_CA)

using: Grails : 2.2.0 Browser : Google Chrome (locale fr_CA)

非常感谢!

推荐答案

我已经搜索并尝试了 2 天.我最终选择定义一个自定义的 PropertyEditorRegistar.这样,我只能为 Double 字段格式修复区域设置.但是,我认为这不是最好的解决方案,因为它将应用于我所有的 Double 字段.但与此同时,它可以很好地完成工作.所以如果有人有更好的解决方案,我会很乐意测试它并更新我的代码.

I've been searching and trying for 2 days. I've finally chosen to define a custom PropertyEditorRegistar. This way, I can fix the Locale only for the Double field format. However, I don't think that it's the best solution because it will be applied to all my Double fields. But in the mean time it does the job pretty well. So if someone have a better solution, I will be glad to test it and update my code.

所以这就是我设置的方式:

So this is how I set this up :

1 - 创建一个新的实现 PropertyEditorRegistrar 的 groovy 类(如果你已经有了,只需在现有的方法中添加该方法包含的部分代码)

1 - Create a new groovy class that implements PropertyEditorRegistrar (if you already have one, just add the part of code contained by the method in the existing one)

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomNumberEditor;

public class CustomDoubleRegistrar implements PropertyEditorRegistrar {

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
            //define new Double format with hardcoded Locale.ENGLISH
        registry.registerCustomEditor(Double.class, 
                 new CustomNumberEditor(Double.class, 
                           DecimalFormat.getInstance(Locale.ENGLISH),true))
    }

}

2- 在 conf/spring/resources.goovy 中定义自定义注册器(当然如果它还没有的话)

2- Define the custom registrar into the conf/spring/resources.goovy (if it's not already there of course)

beans = {
    customPropertyEditorRegistrar(CustomDoubleRegistrar)
}

3- 就是这样,Grails 自动数据绑定可以正常工作

3- That's it, the Grails auto data binding will work fine

Test t = new Test(params); 
//params contains many Double fields with dot '.' as decimal delimiter

不要犹豫,发布更好的解决方案...谢谢

Don't hesitate to post better solutions... Thanks

编辑 1

自 Grails 2.3 起,此解决方案不再有效.如果你还想使用这个方案,你必须把这个配置添加到Config.groovy文件

Since Grails 2.3 this solution is no longer working. If you still want to use this solution, you have to add this configuration to the Config.groovy file

grails.databinding.useSpringBinder = true

或实施新的DataBinding 之一.我尝试了其中的几个,但似乎没有解决十进制分隔符的问题.感谢您发布答案,如果您知道如何...

Or implement one of the new DataBinding. I've tried few of them but nothing seems to solve the decimal delimiter issue. Thank you to post answer if you know how...

编辑 2

自 Grails 2.4+ 起,您可以定义自己的 ValueConverter 以绕过基本的区域设置验证.请注意,您必须删除在初始帖子和编辑 1 中所做的更改.以下是实现自定义 ValueConverter 的方法:

Since Grails 2.4+ you can define your own ValueConverter to bypass the basic Locale validation. Note that you must remove the changes made in the initial post and in the EDIT 1. Here's how to implement the custom ValueConverter:

conf/spring/resources.groovy

// Place your Spring DSL code here
beans = {
    "defaultGrailsjava.lang.DoubleConverter"(DoubleValueConverter)
}

class DoubleValueConverter implements ValueConverter {

    public LongValueConverter() {
    }

    boolean canConvert(value) {
        value instanceof Double
    }

    def convert(value) {
        //In my case returning the same value did the trick but you can define 
        //custom code that takes care about comma and point delimiter...
        return value
    }

    Class<?> getTargetType() {
        return Double.class
    }
}

这篇关于Grails 数据绑定与十进制分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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