SpringMvc 传参类型问题

程序员Joe 发布于 2014/09/22 14:37
阅读 13K+
收藏 0

【开源中国 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));
	}



加载中
0
程序员戊
程序员戊
你controller怎么写的
程序员Joe
程序员Joe
补上了
0
小99
小99
自己扩充一个数据绑定转换类吧
0
杀丶破狼
杀丶破狼
springmvc,表单数据除了日期转成string以外,其他的没做处理,model接受没发现问题
杀丶破狼
杀丶破狼
该评论暂时无法显示,详情咨询 QQ 群:点此入群
程序员Joe
程序员Joe
javaBean里面设置一个float类型的字段传值试试
0
Jack_Q
Jack_Q

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

Jack_Q
Jack_Q
该评论暂时无法显示,详情咨询 QQ 群:点此入群
程序员Joe
程序员Joe
为什么自带的number验证不行呢
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部