1.在UsersControllter.java中
1 @RequestMapping("toDate.do") 2 public String toDate(Date date) { 3 System.out.println(date); 4 return "index"; 5 }
1 @InitBinder 2 public void initBinder(ServletRequestDataBinder binder){ 3 //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 4 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), 5 true)); 6 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <a href="user/register.do?name=zs">链接到List.do</a> 11 12 <form action="user/register.do" method="post"> 13 姓名:<input type="text" name="uname"/><br> 14 密码:<input type="text" name="password"/><br> 15 性别:<input type="text" name="sex"/><br> 16 年龄:<input type="text" name="agee"/><br> 17 地址:<input type="text" name="address"/><br> 18 手机:<input type="text" name="phone"/><br> 19 <input type="submit" value="提交"/> 20 </form> 21 22 </body> 23 </html>
1 @RequestMapping("register.do") 2 public String register(Users users) { 3 System.out.println(users); 4 return "index"; 5 }
2.bean 下的 Users.java 中
1 package com.zhiyou100.wc.bean; 2 3 import java.util.Date; 4 5 import org.springframework.format.annotation.DateTimeFormat; 6 7 public class Users { 8 private String uname; 9 private String password; 10 private String sex; 11 private int age; 12 private String address; 13 private String phone; 14 @DateTimeFormat(pattern="yyyy-MM-dd") 15 private Date brithday; 16 17 public Date getBrithday() { 18 return brithday; 19 } 20 public void setBrithday(Date brithday) { 21 this.brithday = brithday; 22 } 23 public String getUname() { 24 return uname; 25 } 26 public void setUname(String uname) { 27 this.uname = uname; 28 } 29 public String getPassword() { 30 return password; 31 } 32 public void setPassword(String password) { 33 this.password = password; 34 } 35 public String getSex() { 36 return sex; 37 } 38 public void setSex(String sex) { 39 this.sex = sex; 40 } 41 42 public int getAge() { 43 return age; 44 } 45 public void setAge(int age) { 46 this.age = age; 47 } 48 public String getAddress() { 49 return address; 50 } 51 public void setAddress(String address) { 52 this.address = address; 53 } 54 public String getPhone() { 55 return phone; 56 } 57 public void setPhone(String phone) { 58 this.phone = phone; 59 } 60 @Override 61 public String toString() { 62 return "Users [uname=" + uname + ", password=" + password + ", sex=" + sex + ", age=" + age + ", address=" 63 + address + ", phone=" + phone + ", brithday=" + brithday + "]"; 64 } 65 public Users(String uname, String password, String sex, int age, String address, String phone, Date brithday) { 66 super(); 67 this.uname = uname; 68 this.password = password; 69 this.sex = sex; 70 this.age = age; 71 this.address = address; 72 this.phone = phone; 73 this.brithday = brithday; 74 } 75 public Users() { 76 super(); 77 // TODO Auto-generated constructor stub 78 } 79 80 81 }
原文:https://www.cnblogs.com/banzhuanlaowang/p/11455972.html