Spring MVC默认情况下可以对基本类型进行类型转换,例如可以将String转换为Integer,Double,Float等。但是Spring MVC并不能转换日期类型(java.util.Date),如果希望把字符串参数转换为日期类型,必须自定义类型转换器。接下来讲解如何自定义类型转换器。
1 设计表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>一点教程网 - www.yiidian.com</title>
</head>
<body>
<h2>自定义类型转换</h2>
<form action="/param.do" method="post">
用户名:<input type="text" name="username"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2 设计Pojo
User对象:
package com.yiidian.domain;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 用于封装表单数据
* 一点教程网 - www.yiidian.com
*/
public class User {
private String username;
private Date birthday;//这里接收的是java.util.Date类型
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", birthday=" + birthday +
'}';
}
}
3 编写日期类型转换器
package com.yiidian.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 字符串转换日期类型转换器
* 一点教程网 - www.yiidian.com
*/
public class StringToDateConverter implements Converter<String,Date>{
@Override
public Date convert(String source) {
Date date = null;
try {
//使用SimpleDateFormat对页面字符串日期转换为java.util.Date类型
date = new SimpleDateFormat("yyyy-MM-dd").parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
注意:Spring MVC的自定义类型转换器必须实现Converter接口
4 配置自定义类型转换器
<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.扫描Controller的包-->
<context:component-scan base-package="com.yiidian.controller"/>
<!-- 2.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 2.1 页面前缀 -->
<property name="prefix" value="/pages/"/>
<!-- 2.2 页面后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3.创建处理器适配器和处理器映射器-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--4.配置自定义类型转换器-->
<!--4.1 创建类型转换器对象-->
<bean id="stringToDateConverter" class="com.yiidian.converter.StringToDateConverter"/>
<!--4.2 把新建的类型转换器对象加入到工厂中-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="stringToDateConverter"/>
</set>
</property>
</bean>
</beans>
5 编写Controller
package com.yiidian.controller;
import com.yiidian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* 自定义类型转换
* 一点教程网 - www.yiidian.com
*/
@Controller
public class ParamController {
@RequestMapping("/param.do")
public String save(User user){
System.out.println("用户名:"+user.getUsername());
System.out.println("生日:"+user.getBirthday());
return "success";
}
}
6 运行测试
控制台输出: