Scenario: When you use bind data in a method, you open the link and a 400 error occurs.
@RequestMapping(value"editItemSubmit") publiceditItemSubmit(int id, Items item) { itemService.updateItemFromId(id, item); return"redirect:queryItemlList"; }
publicclass Items { privateint id; private String name; privatefloat price; private String pic; private Date createtime; private String detail;
Note: There is a date type in my bean, but spring MVC does not convert the string type of the previous paragraph to the sorted date type, so this error occurs.
WORKAROUND: Use custom parameter bindings (Converter converter mode)
- Write the converter of the response to implement the fixed interface:
The first parameter is the original type, the second parameter is the converted type public class Customdateconverter implements converter <string , date > { @Override Span class= "Hljs-keyword" >public Date convert (String s) {try { return new SimpleDateFormat ( "Yyyy-mm-dd HH:mm:ss" ). Parse (s); } catch (parseexception e) {e.printstacktrace (); } return null ; }}
Configure the Format conversion Service Factory Bean:formattingconversionservicefactorybean in the Springmvc.xml configuration file
id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <propertyname="converters"> <list> class="xyz.zhulei.spring_mybatis.controller.convert.CustomDateConverter"/> </list> </property> </bean>
Then add:
<mvc:annotation-driven conversion-service="conversionService"/>
When you're done, you can automatically complete the string-to-date conversion.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring MVC data Binding 400 error