SpringMVC用POST方式提交数据(包括含时间)时遇到The request sent by the client was syntactically incorrect.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36640903/article/details/85062751

        对于一个刚入手SSM的小白来说学习可谓是步履维艰,这不在修改表单数据并提交时就遇到了The request sent by the client was syntactically incorrect.问题。在网上看了好多回答,有说jar包没对的,也有说是表单数据格式不对的(要指定为enctype="multipart/form-data")。这些方法都试了,但问题还是没得到解决。最后发现我的问题原来是在表单里提交时间的格式是字符串String,但是后台处理的是Date格式,所以就出现了参数绑定错误,要先将String转换为Date。解决方案如下,在相应的Controller中加上下面这个方法(必须有注解):

@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		//日期格式转换
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // CustomDateEditor为自定义日期编辑器
	}

方法所在类为:

@Controller
@RequestMapping("/items")
public class ItemsController {
    @RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Items items) {
		itemsService.saveOrUpdate(items);
		return "redirect:list.do";
	}
}

SSM整合第一天,加油!!!

猜你喜欢

转载自blog.csdn.net/qq_36640903/article/details/85062751