使用registercustomereditor在数字列表上弹出数字格式 [英] Spring numberformatting with registercustomereditor on list of numbers

查看:488
本文介绍了使用registercustomereditor在数字列表上弹出数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spring中注册一个customerEditor以格式化一个给定数字格式的数字实例,很容易将其应用到jsp中的一个特定字段,例如:

When registering a customerEditor in spring to format a number with a given numberFormat instance, it is easy to apply this to a specific field in the jsp, e.g.:

NumberFormat numberFormat = getNumberFormat(0, 0, 2);
PropertyEditor propertyEditor = 
    new CustomNumberEditor(Double.class, numberFormat, true);
binder.registerCustomEditor(Double.class, "myDoubleField", propertyEditor);

这将导致基于应用程序区域设置的正确格式的数字(关于逗号和点为千位/小数分隔符)和分隔符之前和之后的指定小数。

This will result in a correct format of the number based on the applications locale (regarding commas and dots for thousand/decimal separator) and with the specified decimals before and after the seperator.

但是,如果我有一个包含二进制的未知大小的列表,我可以如何在聪明的方式我当然可以在列表中列出每个条目并注册一个列表,但这似乎是麻烦和错误的。

However, if I have a list of unknown size containing doubles how can I format these in a smart way? I could of course run through the list and register one for each entry in the list, but it seems cumbersome and wrong.

由于spring绑定到带有指示的列表中,字段名称将具有名称,如myDoubleField [0] .... myDoubleField [n] 因此使它变得很难...

Since spring is binding to lists with indicises the field names would have names like "myDoubleField[0] .... myDoubleField[n]" which therefore makes it hard...

有没有一个容易的解决方法?还是有一个解决方法?

Is there an easy workaround for this? Or is there a workaround at all?

提前感谢很多,希望有人能指出我正确的方向!

Thanks a lot in advance, I hope someone can point me in a correct direction!

推荐答案

通过使用全局PropertyEditorRegistrar替换旧的繁琐的注册自定义编辑器的方法来解决。
在构造函数中初始化控制器:

Solved by replacing old cumbersome way of registering custom editors, by using a global PropertyEditorRegistrar. Initializing controllers with this in constructor:

public myController(PropertyEditorRegistrar customPropertyEditorRegistrar) {
    this.customPropertyEditorRegistrar = customPropertyEditorRegistrar;
}

并在initBinder中注册:

and registering this in initBinder:

@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)   throws Exception {
    customPropertyEditorRegistrar.registerCustomEditors(binder);
}

强制所有元素按照CustomerPropertyEditorRegistrar中指定的方式进行格式化。 >
例如加倍:

Forces all elements to be formatted in the way specified in the CustomerPropertyEditorRegistrar.
Eg. with doubles:

public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
    // Double
    PropertyEditor doubleEditor = getLocaleBasedNumberEditor(Double.class, true);
    registry.registerCustomEditor(double.class, doubleEditor);
    registry.registerCustomEditor(Double.class, doubleEditor);
}

如果需要另一种格式,可以以旧的方式覆盖特定字段对于特定字段。

Specific fields can the be overwritten in the old manner, if another formatting is desired for a specific field.

// Hoof

这篇关于使用registercustomereditor在数字列表上弹出数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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