Springmvc的jar包下载:https://pan.baidu.com/s/1Uu5R96z4LwwtydGq4B60Xg 密码:8c3n
src/springmvc.xml(Springmvc核心配置文件,配置类型转换器,为适配器指定转换器):
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解扫描。 扫描 @Controler @Service等注解 -->
<context:component-scan base-package="com.xxx"/>
<!-- 注解驱动; 指定处理器映射器、处理器适配器; 为适配器指定转换器(适配器会根据需要的接收类型对请求参数自动进行转换) -->
<mvc:annotation-driven conversion-service="conversionServiceFactory"/>
<!-- 配置Converter转换工厂(对请求参数进行处理转换)。 (可以转换日期格式;可以对请求参数前后空格进行处理等) -->
<bean id="conversionServiceFactory" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 可以配置多个转换器-->
<property name="converters">
<list>
<bean class="com.xxx.springmvc.conversion.DateConveter"/> <!-- 自定义的日期格式转换器 -->
</list>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图(jsp页面)请求路径的前缀 -->
<property name="suffix" value=".jsp"/> <!-- 后缀 -->
</bean>
</beans>
DateConveter.java(自定义的类型转换器,实现Converter接口):
package com.xxx.springmvc.conversion;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
* 转换成指定格式的日期类型
* String : 请求参数的类型
* Date : 转换后的类型
*/
public class DateConveter implements Converter<String, Date>{ //实现Converter接口; 泛型<请求参数类型,转换后的类型>
@Override
public Date convert(String source) {
try {
if(source != null){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(source);
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
src/springmvc.xml(配置类型转换器,配置方式二(了解)):
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解扫描。 扫描 @Controler @Service等注解 -->
<context:component-scan base-package="com.xxx"/>
<!-- 处理器映射器(HandlerMapping) -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 处理器适配器(HandlAdapter)。 为适配器指定转换器(适配器会根据需要的接收类型对请求参数自动进行转换) -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 必须手动配置处理器映射器和处理器适配器。不能通过驱动配置 -->
<!-- <mvc:annotation-driven /> -->
<!-- 配置webBinder -->
<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref=" conversionServiceFactory " />
</bean>
<!-- 转换器配置 -->
<bean id="conversionServiceFactory" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.xxx.springmvc.conversion.DateConveter" /> <!-- 自定义的日期格式转换器 -->
</set>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图(jsp页面)请求路径的前缀 -->
<property name="suffix" value=".jsp"/> <!-- 后缀 -->
</bean>
</beans>