【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
最一开始做了一个普通的表单提交。
没问题,后来多了一个日期类型,如果页面填了,保存没,问题,为空,保存时会报类型无法转换的错误,查阅资料后,在基类controller中加入
/** * 用于处理Date类型参数处理 * @return */ @InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }后解决,先后现在有float类型
我用同样的方法,
// binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, false));
// binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, false));
// binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false));
不管怎么样都是不行,这是为什么呢?
填了保存正常,为空是就报错:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'page_person' on field 'sg': rejected value []; codes [typeMismatch.page_person.sg,typeMismatch.sg,typeMismatch.float,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [page_person.sg,sg]; arguments []; default message [sg]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'float' for property 'sg'; nested exception is java.lang.NumberFormatException: empty String]
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'page_person' on field 'sg': rejected value []; codes [typeMismatch.page_person.sg,typeMismatch.sg,typeMismatch.float,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [page_person.sg,sg]; arguments []; default message [sg]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'float' for property 'sg'; nested exception is java.lang.NumberFormatException: empty String]
跪求大神,在线等
补controller代码:
/** * add、update * @param czPerson * @param person * @return */ @ResponseBody @RequestMapping(value = "/add") public String add(@ModelAttribute Page_person person,@ModelAttribute Page_person_cz czPerson) { if(StringSimple.isNullOrEmpty(person.getId())){//如果是新增 if(person_czService.noExist(person.getZjhm())){ return JsonSimple.toJSONString(Utils.createMsg("fail", "证件号码已经存在!", false)); } person_czService.add(person, czPerson); }else{//修改 czPerson.setId(request.getParameter("czid")); czPerson.setPersonid(person.getId()); person_czService.update(person, czPerson); } return JsonSimple.toJSONString(Utils.createMsg("success", "保存成功!", true)); }
自定义一个CustomEditor类型,来处理null