@ModelAttribute parameter binding type conversion error (solved submit to background bigdecimal including, "" cause problems foreground conversion failed return error 400)

  • Add the controller in

@InitBinder
public void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(BigDecimal.class, new BigDecimalEditor());//BigDecimalEditor为转换器
}

  • Add class

/**
 * created by lxy 2019年2月12日10:56:50
 */
public class BigDecimalEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        if (StringUtils.isNotEmpty(text)) {
            if (text.contains(",")) {
               text=text.replaceAll(",", "");
            }
        } else {
            text = "0";
        }
        setValue(new BigDecimal(text));
    }
}

 

Guess you like

Origin blog.csdn.net/i929479824/article/details/91418493
Recommended