星空网 > 软件开发 > Java

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

系统: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   @Override49   public String toString() {50     return "User [createDate=" + createDate + ", userId=" + userId51         + ", 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 @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 }

上面的代码不仅仅有日期格式的编辑器,还有基础类型的编辑器,这样就解决了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 }


以上,问题解决。

 




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

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

PJ Masks开始维权,GBC向侵权卖家索赔200万美金!:https://www.ikjzd.com/articles/4223
如何有效降低亚马逊中的FBA成本:https://www.ikjzd.com/articles/423
VP评价门槛提高,买家购买折扣产品可能无法留评!:https://www.ikjzd.com/articles/4230
Etsy公布18年Q2财报,收入同比增长30.2%!:https://www.ikjzd.com/articles/4235
VP评论政策收紧,刷单、购买折扣产品均不能获得VP评论!:https://www.ikjzd.com/articles/4236
值得关注的亚马逊日本站:https://www.ikjzd.com/articles/424
宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流