form表单传日期400

来源:互联网 发布:mysql union or in 编辑:程序博客网 时间:2024/06/08 11:36

1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test here</title>
</head>
<body>
<form method="post" action="testAdd" id="form">

 <input type="text" name="users[0].id"/>
 <input type="text" name="users[0].userName"/>
<input type="text" name="users[0].time"/>
<input type="text" name="users[1].id"/>
 <input type="text" name="users[1].userName"/>
 <input type="text" name="users[1].time"/>
 <input type="submit" value="submit"/>
</form>
</body>
</html>

2.对象

package com.lucene.model;


import java.util.Date;


public class User {
private Integer id;
private String userName;
private Date time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}


}

package com.lucene.model;


import java.util.List;


public class UserModel {
private List<User> users;


public List<User> getUsers() {
return users;
}


public void setUsers(List<User> users) {
this.users = users;
}
}

3控制器

@RequestMapping(value = "/testAdd", method ={RequestMethod.POST})
public String testAdd(UserModel userModel) {
return "test";
}
@InitBinder    
public void initBinder(WebDataBinder binder) {    
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
       simpleDateFormat.setLenient(false);    
       binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));    
}  

4.解决在控制器增加如下方法

@InitBinder    
public void initBinder(WebDataBinder binder) {    
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
       simpleDateFormat.setLenient(false);    
       binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));    
}  


5.结果


原创粉丝点击