@DateTimeFormat(pattern="yyyy-MM-dd")
返回的时候java.util.Date
pattern="yyyy-MM-dd"必须要和页面中的日期格式对应。
contraller层:
package com.chenk.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.chenk.pojo.Users; @Controller public class TimeController { @RequestMapping("/addUserController") public String addUser(Users user,Model model){ model.addAttribute("user", user); System.out.println(user); return "showUser"; } }
Entity
package com.chenk.pojo; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class Users { private String name; private String age; //备用yyyy-MM-dd HH:mm:ss @DateTimeFormat(pattern="yyyy-MM-dd") private Date birth; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Users [name=" + name + ", age=" + age + ", birth=" + birth + "]"; } }