使用@DateTimeFormat(pattern="yyyy-MM-dd"), 可以规定页面提交的日期格式
package com.atchina.pojo;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class Employee {
private String id;
private String name;
// 规定页面提交的日期格式
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getId() {
return id;
}
public Employee() {
}
public Employee(String id, String name, Integer gender, Address address) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.address = address;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
private Integer gender;
private Address address;
}
写自定义类型转换器,就使用FormattingConversionServiceFactoryBean来注册
既具有类型转换,又有格式化功能
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.atchina"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 静态资源可以访问了 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 写自定义类型转换器,就使用FormattingConversionServiceFactoryBean来注册
既具有类型转换,又有格式化功能 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.atchina.component.StringToEmployeeConverter"></bean>
</set>
</property>
</bean>
</beans>