系统:WIN8.1
数据库:Oracle 11GR2
开发工具:MyEclipse 8.6
框架:Spring3.2.9、SpringMVC3.2.9、MyBatis3.2.8
使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 。下面是解决方案的演示示例:
这个是实体类,里面createDate就是java.util.Date类型
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 @Override 49 public String toString() { 50 return "User [createDate=" + createDate + ", userId=" + userId 51 + ", userName=" + userName + "]"; 52 } 53 }
页面代码
1 <form action="regUser" method="post"> 2 userName:<input type="text" name="userName"/><br> 3 createDate:<input type="text" name="createDate"/><br> 4 double类型:<input type="text" name="dd"/><br> 5 <input type="submit" value="注册"> 6 </form>
因为对于原生基本类型的form表单绑定,会出错。需要指定具体的类型编辑器。用法如下:首先在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器。剩下的控制器都继承该类。CustomDateEditor spring自己已经提供了。代码如下:
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 @Controller 15 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 }
上面的代码不仅仅有日期格式的编辑器,还有基础类型的编辑器,这样就解决了SpringMVC中controller方法接受参数的时候,基础类型报错的问题了。
下面是测试用代码,继承BaseController之后就可以直接运行了。接受的参数有实体类和基础类型。
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 }
以上,问题解决。然后我们切换第二种方式,删除 BaseController 这个类,直接在User实体类中的 createDate字段上加上注解 , 注意第10行代码:
1 import java.util.Date; 2 3 import org.springframework.format.annotation.DateTimeFormat; 4 5 public class User { 6 7 private int userId; 8 private String userName; 9 10 @DateTimeFormat(pattern="yyyy-MM-dd") 11 private Date createDate; 12 13 public User() {} 14 15 public User(int userId, String userName, Date createDate) { 16 super(); 17 this.userId = userId; 18 this.userName = userName; 19 this.createDate = createDate; 20 } 21 22 public User(String userName, Date createDate) { 23 super(); 24 this.userName = userName; 25 this.createDate = createDate; 26 } 27 28 public int getUserId() { 29 return userId; 30 } 31 32 public void setUserId(int userId) { 33 this.userId = userId; 34 } 35 36 public String getUserName() { 37 return userName; 38 } 39 40 public void setUserName(String userName) { 41 this.userName = userName; 42 } 43 44 public Date getCreateDate() { 45 return createDate; 46 } 47 48 public void setCreateDate(Date createDate) { 49 this.createDate = createDate; 50 } 51 52 @Override 53 public String toString() { 54 return "User [createDate=" + createDate + ", userId=" + userId 55 + ", userName=" + userName + "]"; 56 } 57 }
这样也可以解决日期格式报400问题。而且不管页面是否有数据都可以正常使用。