开发者社区> 问答> 正文

SpringMvc 传参类型问题?报错

最一开始做了一个普通的表单提交。

没问题,后来多了一个日期类型,如果页面填了,保存没,问题,为空,保存时会报类型无法转换的错误,查阅资料后,在基类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));
	}



展开
收起
爱吃鱼的程序员 2020-06-14 21:36:26 495 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    自定义一个 CustomEditor类型,来处理null

    回复 @程序员V:debug下源码看看,估计没验证null的情况。为什么自带的number验证不行呢springmvc,表单数据除了日期转成string以外,其他的没做处理,model接受没发现问题 @程序员Vfloat没试,用的是double,传值是可以的javaBean里面设置一个float类型的字段传值试试自己扩充一个数据绑定转换类吧你controller怎么写的补上了
    2020-06-14 21:36:44
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多