系统: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类型

 import java.util.Date;

 public class User {

     private int userId;
private String userName;
private Date createDate; public User() {} public User(int userId, String userName, Date createDate) {
super();
this.userId = userId;
this.userName = userName;
this.createDate = createDate;
} public User(String userName, Date createDate) {
super();
this.userName = userName;
this.createDate = createDate;
} public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public Date getCreateDate() {
return createDate;
} public void setCreateDate(Date createDate) {
this.createDate = createDate;
} @Override
public String toString() {
return "User [createDate=" + createDate + ", userId=" + userId
+ ", userName=" + userName + "]";
}
}

页面代码

   <form action="regUser" method="post">
userName:<input type="text" name="userName"/><br>
createDate:<input type="text" name="createDate"/><br>
double类型:<input type="text" name="dd"/><br>
<input type="submit" value="注册">
</form>

因为对于原生基本类型的form表单绑定,会出错。需要指定具体的类型编辑器。用法如下:首先在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器。剩下的控制器都继承该类。CustomDateEditor spring自己已经提供了。代码如下:

 import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder; import sun.beans.editors.DoubleEditor;
import sun.beans.editors.FloatEditor;
import sun.beans.editors.IntEditor;
import sun.beans.editors.LongEditor; @Controller
public class BaseController { @InitBinder
public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
binder.registerCustomEditor(int.class, new IntEditor());
binder.registerCustomEditor(long.class, new LongEditor());
binder.registerCustomEditor(double.class, new DoubleEditor());
binder.registerCustomEditor(float.class, new FloatEditor());
} }

上面的代码不仅仅有日期格式的编辑器,还有基础类型的编辑器,这样就解决了SpringMVC中controller方法接受参数的时候,基础类型报错的问题了。

下面是测试用代码,继承BaseController之后就可以直接运行了。接受的参数有实体类和基础类型。

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.kickstarter.entity.User; @Controller("userController")
public class UserController extends BaseController{ @RequestMapping(value="regUser")
public String dateTest(User user , double dd){ System.out.println( user.toString() );
System.out.println( dd );
return "index";
}
}

以上,问题解决。然后我们切换第二种方式,删除 BaseController 这个类,直接在User实体类中的 createDate字段上加上注解 , 注意第10行代码:

 import java.util.Date;

 import org.springframework.format.annotation.DateTimeFormat;

 public class User {

     private int userId;
private String userName; @DateTimeFormat(pattern="yyyy-MM-dd")
private Date createDate; public User() {} public User(int userId, String userName, Date createDate) {
super();
this.userId = userId;
this.userName = userName;
this.createDate = createDate;
} public User(String userName, Date createDate) {
super();
this.userName = userName;
this.createDate = createDate;
} public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public Date getCreateDate() {
return createDate;
} public void setCreateDate(Date createDate) {
this.createDate = createDate;
} @Override
public String toString() {
return "User [createDate=" + createDate + ", userId=" + userId
+ ", userName=" + userName + "]";
}
}

这样也可以解决日期格式报400问题。而且不管页面是否有数据都可以正常使用。

SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法的更多相关文章

  1. SpringMVC提交数据遭遇基础类型和日期类型报400错误解决方法

    使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 .下面是解决方案的演示示例: 这个是实体类,里面createDate就是java ...

  2. SpringMVC:提交日期类型报400错误解决方法

    方法1:可以使用@ControllerAdvice增强Controller @ControllerAdvice public class BaseControllerAdvice { // 初始化绑定 ...

  3. SpringMVC + Spring + MyBatis 学习笔记:遭遇order by 排序问题

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 用MyBatis写排序 ...

  4. SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...

  5. SpringMVC + Spring + MyBatis 学习笔记:为MyBatis增加打印SQL功能 (最简化配置)

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 1.以下jar包拷贝到 ...

  6. SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...

  7. ajax post提交form表单 报400错误 解决方法

    昨天晚上做项目遇到了一个奇怪的问题,我用ajax提交一个form表单,后台Java方法用的是一个实体接,但是他根本不进方法体中,直接给我一个400的错误,一开始我以为是我路径的问题(尴尬),结果直接访 ...

  8. mybatis 批量update报语法错误解决方法

    1.为什么会报语法错误 原因:在 *.xml文件内使用了循环,在mybatis中默认是不允许使用批量修改. <update id="setMaxMin" parameterT ...

  9. 1.4(Spring MVC学习笔记)JSON数据交互与RESTful支持

    一.JSON数据交互 1.1JSON简介 JSON(JavaScript Object Notation)是一种数据交换格式. 1.2JSON对象结构 {}代表一个对象,{}中写入数据信息,通常为ke ...

随机推荐

  1. JavaScript —— 局部变量和全局变量

    JS的全局变量有3种声明方式: 1.Function 外 var v_myVar; 2.Function 内 v_myVar; 3.window.v_myVar window.v_myVar 全局变量 ...

  2. Using dblink in Postgres

    select contractid from tcim_s_enterprice EXCEPT select contractid from dblink ( 'host=172.16.51.25 p ...

  3. OpenStack介绍

    简介 OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管 ...

  4. 在DirectX 中进行2D渲染

    http://flcstudio.blog.163.com/blog/static/756035392008115111123672/ 最近,我看到很多关于DirectX8在最新的API中摒弃Dire ...

  5. java.util.logging.Logger使用详解

    一.创建Logger对象   static Logger getLogger(String name)           为指定子系统查找或创建一个 logger. static Logger ge ...

  6. MVC 中使用扩展方法

     扩展方法(Extension Method)是给那些不是你拥有.因而不能直接修改的类添加方法的一种方便的办法. 一.使用扩展方法 1.定义一个购物车的类-ShoppingCart using Sys ...

  7. UVa 12100 (模拟) Printer Queue

    用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...

  8. CodeForces ZeptoLab Code Rush 2015

    拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...

  9. splay入门

    在比较了网上的几份模板的速度之后,发现指针版明显快了很多,但是一敲起来....各种不习惯...所以还是学的hzwer 的数组版... bzoj3223:维护reverse操作就可以了 #include ...

  10. Windows 8获取开发者账户

    使用PowerShell获取开发者账户,可以在本地调试Metro APP C:\PS> Show-WindowsDeveloperLicenseRegistration //安装licence ...