SpringMVC parameter binding-date format conversion
1. SpringMVC does not support the conversion of date strings on the page to Date in the background by default
- Front Form
出生日期<input type="date" name="birthday2"/><br/>
- Correspondence between entity class and form submission data (string)
2. There are two ways to solve it
(1) Use annotations to achieve
Annotation date type converter
@DateTimeFormat("格式")
Just modify the birthday2 date attribute and add a @DateTimeFormat("format")
public class Person {
private int id;
private String username;
private String password;
private String city;
private Birthday birthday;
@DateTimeFormat(pattern ="yyyy-MM-dd")
private Date birthday2;
}
How to write the format behind the comment? Write according to the actual data sent by the request. For
example: "2020-10-14" is written as "yyyy-MM-dd"
(2) The second kind of self-write conversion class, configure to springMVC (global date converter)
The implementation steps of writing a custom date type converter:
- Write a custom converter to implement the Converter rewrite method and convert
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//1:将页面上提交的日期字符串,转成Date对象
public class DateTimeFormatConverter implements Converter<String, Date> {
public Date convert(String s) {
System.out.println("convert "+s);
//2:转换器
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(s);//2020-10-14
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
- Configure the conversion factory in springmvc.xml and set our converter to the converters collection
<!--注册转换器,并注入自定义的转换器-->
<bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!--配置自己自定义的转换器-->
<bean id="dateTimeFormatConverter" class="cn.cyl.util.DateTimeFormatConverter"></bean>
</set>
</property>
</bean>
- Mount the conversion factory object to the processor adapter (mount to the annotation driver)
<!--springmvc注解驱动标签,引入转换器-->
<mvc:annotation-driven conversion-service="formattingConversionService"/>
3. Conversion of background date data to page
Background Date data
List<Person> list = new ArrayList<Person>();
list.add(new Person(1,"jack","123456","北京",new Birthday(1998,1,1),new Date()));
list.add(new Person(2,"rose","123456","changsha",new Birthday(1998,2,2),new Date()));
Front Form
<c:forEach items="${list}" var="item" >
<tr>
<%--此格式输出的是系统日期格式--%>
<td>${item.birthday2}</td>
</tr>
</c:forEach>
Front display
- Add date formatting tags to the foreground form
(1) Import tags
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
(2) Call date formatting tag
<fmt:formatDate value="${item.birthday2}" pattern="yyyy年MM月dd日"/>
Front Form
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:forEach items="${list}" var="item" >
<tr>
<%--调用日期格式化标签--%>
<td><fmt:formatDate value="${item.birthday2}" pattern="yyyy年MM月dd日"/></td>
</tr>
</c:forEach>
Front display