Grails数据绑定,带十进制分隔符 [英] Grails databinding with decimal delimiter
问题描述
当我使用双表单字段上的十进制分隔符使用grail自动数据绑定ex:Test t = new Test(params)时出现问题。经过几次搜索后,我发现它与浏览器语言环境有关。例如:
如果输入小数点后用小数点3.45分隔的数字,则不起作用(数字的小数部分被忽略)。它在我的数据库中存储3.0
如果我使用逗号3,45作为小数点分隔符,则一切正常。 Grails将3.45存储在数据库中。
问题是没有人用逗号分隔符输入数字。 (甚至数字小键盘在设置为fr_CA时输入点。)
我已经发现了一些解决方案,比如注册自定义数字编辑器(当你有很多应用程序时有点痛苦)或将全局localeResolver设置为en_US(最后一个不执行这项工作,因为我放弃了我应用程序的所有国际化功能)。
那么,有人有一个简单的解决方案来解决这个问题?
使用:
Grails:2.2.0
浏览器:Google Chrome(locale fr_CA)
非常感谢!
我一直在寻找并尝试2天。我终于选择定义一个自定义的PropertyEditorRegistar。这样,我可以修复仅适用于Double字段格式的区域设置。但是,我不认为这是最好的解决方案,因为它将应用于我的所有Double字段。但同时它做得很好。所以如果有人有更好的解决方案,我会很乐意测试它并更新我的代码。
所以这就是我设置它的方式:
1 - 创建一个实现PropertyEditorRegistrar的新groovy类(如果您已经有一个,只需将方法中包含的代码部分添加到现有的代码中)
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实现PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry){
//用硬编码的Locale定义新的Double格式。 ENGLISH
registry.registerCustomEditor(Double.class,
)新的CustomNumberEditor(Double.class,
DecimalFormat.getInstance(Locale.ENGLISH),true))
}
<2>将定制注册器定义到conf / spring / resources.goov中(if它不是已经存在)
beans = {
customPropertyEditorRegistrar(CustomDoubleRegistrar)
}
<3->就是这样,Grails自动数据绑定可以正常工作
Test t = new Test(params);
// params包含许多带dot'。'的Double字段作为小数分隔符
Don'毫不犹豫地发布更好的解决方案......
谢谢
编辑1
由于Grails 2.3这个解决方案不再有效。如果您仍想使用此解决方案,则必须将此配置添加到 Config.groovy 文件
grails.databinding中。 useSpringBinder = true
或者实现一个新的 DataBinding 。我尝试了其中的几个,但似乎没有解决小数分隔符问题。谢谢你发布答案,如果你知道如何......
编辑2
从Grails 2.4+开始,您可以定义自己的ValueConverter以绕过基本的区域设置验证。请注意,您必须删除在初始文章和编辑1 中所做的更改。以下是如何实现自定义ValueConverter:
conf / spring / resources.groovy
//将您的Spring DSL代码放在这里
beans = {
defaultGrailsjava.lang.DoubleConverter(DoubleValueConverter)
}
class DoubleValueConverter实现ValueConverter {
public LongValueConverter(){
}
boolean canConvert(value){
value instanceof Double
}
def convert(value){
//在我的例子中,返回相同的值的方法不过了,但是您可以定义
//自定义代码,用于处理逗号和点分隔符...
返回值
}
Class<?> getTargetType(){
return Double.class
}
}
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.
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
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.
The problem is that nobody enter number with comma delimiter. (even the numpad enter a dot '.' when set to fr_CA)
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?
using: Grails : 2.2.0 Browser : Google Chrome (locale fr_CA)
Thanks a lot!
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 - 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- Define the custom registrar into the conf/spring/resources.goovy (if it's not already there of course)
beans = {
customPropertyEditorRegistrar(CustomDoubleRegistrar)
}
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
EDIT 1
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
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...
EDIT 2
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屋!