您的位置:首页 > 编程语言 > Java开发

SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇日期格式转化报400错误解决方法

2015-12-23 12:54 946 查看
系统: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 }


以上,问题解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc