SpringMVC + Spring + MyBatis Study Notes: solution to error 400 when data is submitted in the date format, springmvcmybatis
System: WIN8.1
Database: Oracle 11GR2
Development Tool: MyEclipse 8.6
Framework: Spring3.2.9, SpringMVC3.2.9, and MyBatis3.2.8
When SpringMVC is used for development, if the page contains data in the Date format and the backend accept is also java. util. Date, error 400 is reported. The following is an example of a solution:
This is an object class. In this example, createDate is of the java. util. Date type.
1 import java.util.Date; 2 3 public class User { 4 5 private int userId; 6 private String userName; 7 private Date createDate; 8 9 public User() {}10 11 public User(int userId, String userName, Date createDate) {12 super();13 this.userId = userId;14 this.userName = userName;15 this.createDate = createDate;16 }17 18 public User(String userName, Date createDate) {19 super();20 this.userName = userName;21 this.createDate = createDate;22 }23 24 public int getUserId() {25 return userId;26 }27 28 public void setUserId(int userId) {29 this.userId = userId;30 }31 32 public String getUserName() {33 return userName;34 }35 36 public void setUserName(String userName) {37 this.userName = userName;38 }39 40 public Date getCreateDate() {41 return createDate;42 }43 44 public void setCreateDate(Date createDate) {45 this.createDate = createDate;46 }47 48 @Override49 public String toString() {50 return "User [createDate=" + createDate + ", userId=" + userId51 + ", userName=" + userName + "]";52 }53 }
Page code
1 <form action = "regUser" method = "post"> 2 userName: <input type = "text" name = "userName"/> <br> 3 createDate: <input type = "text" name = "createDate"/> <br> 4 double type: <input type = "text" name = "dd"/> <br> 5 <input type = "submit" value = "register"> 6 </form>
An error occurs when binding form forms of the native basic type. You need to specify a specific type editor. The usage is as follows: first add the method initBinder in BaseController, and use annotation @ InitBinder to annotate, then spring mvc will first register these editors before binding the form. All the remaining controllers inherit the class. CustomDateEditor spring already provides it. The Code is as follows:
1 import java.text.SimpleDateFormat; 2 import java.util.Date; 3 4 import org.springframework.beans.propertyeditors.CustomDateEditor; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.WebDataBinder; 7 import org.springframework.web.bind.annotation.InitBinder; 8 9 import sun.beans.editors.DoubleEditor;10 import sun.beans.editors.FloatEditor;11 import sun.beans.editors.IntEditor;12 import sun.beans.editors.LongEditor;13 14 @Controller15 public class BaseController {16 17 @InitBinder 18 public void initBinder(WebDataBinder binder) { 19 20 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); 21 binder.registerCustomEditor(int.class, new IntEditor());22 binder.registerCustomEditor(long.class, new LongEditor()); 23 binder.registerCustomEditor(double.class, new DoubleEditor()); 24 binder.registerCustomEditor(float.class, new FloatEditor()); 25 }26 27 28 }
The above Code not only includes the date format editor, but also the basic type editor. This solves the problem of basic type error when the controller Method in SpringMVC accepts parameters.
The following is the code used for testing. After inheriting the BaseController, you can run it directly. The accepted parameters include the object class and basic type.
1 import org.springframework.stereotype.Controller; 2 import org.springframework.web.bind.annotation.RequestMapping; 3 4 import com.kickstarter.entity.User; 5 6 @Controller("userController") 7 public class UserController extends BaseController{ 8 9 @RequestMapping(value="regUser")10 public String dateTest(User user , double dd){11 12 System.out.println( user.toString() );13 System.out.println( dd );14 return "index"; 15 }16 }
Above, solve the problem.