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

时间:2023-03-09 08:10:10
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类型

 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问题。而且不管页面是否有数据都可以正常使用。