BigDecimal中的Spring MVC控制器NumberFormat注释模式问题 [英] Spring MVC Controller NumberFormat Annotation Pattern Issue in BigDecimal

查看:901
本文介绍了BigDecimal中的Spring MVC控制器NumberFormat注释模式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧控制器,它需要多个BigDecimal RequestParams.

I have a spring controller which is taking multiple BigDecimal RequestParams.

我的应用程序语言环境是en_US,但是仅对于此控制器方法,我需要在de_DE语言环境中绑定并转换这些BigDecimal参数(即,#.###,##> DOT用于分组,而COMMA用于十进制分隔符).

My application locale is en_US but just for this controller method I need to bind and convert these BigDecimal parameters in de_DE locale (ie. #.###,## > DOT for grouping and COMMA for decimal separator).

这些BigDecimal值来自UI文本框,并且已经采用de_DE格式.这是我的控制器代码,失败并出现以下错误:

These BigDecimal values are coming from the UI text boxes and they are already in the de_DE format. Here is my controller code which is failing with the following error:

无法将类型'java.lang.String'的值转换为所需的类型 'java.math.BigDecimal';嵌套的异常是 java.lang.NumberFormatException"

"Failed to convert value of type 'java.lang.String' to required type 'java.math.BigDecimal'; nested exception is java.lang.NumberFormatException"

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView create(@RequestParam("referenceNumber") String referenceNumber, @RequestParam("startDate") @DateTimeFormat(pattern="dd-MM-yyyy") Date startDate, @RequestParam("amount1") @NumberFormat(pattern = "#.###,##") BigDecimal amount1, @RequestParam("amount2") @NumberFormat(pattern = "#.###,##") BigDecimal amount2) {

    // Do something and return

}

Spring某种程度上忽略了我的数字格式模式.请注意,DateTimeFormat批注可以按预期工作.以正确的形式解析startDate参数.

Spring somehow ignores my numberformat pattern. Please note that DateTimeFormat annotation works as expected; parsing the startDate parameter in correct form.

任何帮助将不胜感激.

谢谢.

推荐答案

您可以使用

You can use PropertyEditorSupport to handle the form input as follows:

创建扩展PropertyEditorSupport的类,以将从客户端接收的字符串转换为

Create class extending PropertyEditorSupport to convert String received from client to BigDecimal, for example:

import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class BigDecimalEditor extends PropertyEditorSupport {

    public void setAsText(String text) {
        NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMAN);
        try {
            Number number = formatter.parse(text);
            BigDecimal bigDecimal = BigDecimal.valueOf(number.doubleValue());
            setValue(bigDecimal);
        } catch (ParseException e) {
            // handle exception here
        }
    }
}

并将其与控制器绑定,如下:

And bind it with the controller, as:

@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(BigDecimal.class, new BigDecimalEditor());
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView create(
            @RequestParam("amount") @NumberFormat(pattern = "#.###,##") BigDecimal amount) {
        System.out.println(amount);
        return new ModelAndView();
    }
}

这篇关于BigDecimal中的Spring MVC控制器NumberFormat注释模式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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