用于格式转换
package com.example.demo.model;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class TUser {
private Integer userId;
private String userName;
private String password;
private String phone;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
JSP页面:
<form action="testFormat" method="post">
<input type="text" name="date">
<input type="submit" value="数据格式化">
</form>
后台方法:
@RequestMapping("/testFormat")
public String testFormat(Address address){
System.out.println(address);
return "success";
}
实体类:
//时间
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date;
//浮点数
@NumberFormat(pattern="#,###,###.#")
private Float ind;
jsp页面输入:2017-1-1,后台的实体类可以把它转换成Date类型。
springmvc配置文件需要加入:
<mvc:annotation-driven></mvc:annotation-driven>
经过测试,发现要让注解起效,那么在mvc:annotation-driven这个标签里面就不能在加其它属性,这里只测试了自定义类型转换,发现用了自定义类型转换后,那么对数据进行格式的注解就用不了了。
---------------------
作者:程序搬砖员
来源:CSDN
原文:https://blog.csdn.net/java_xuetu/article/details/80066588
版权声明:本文为博主原创文章,转载请附上博文链接!