全局时间转换器

来源:互联网 发布:无间道2 知乎 编辑:程序博客网 时间:2024/06/03 14:54

一.当配置文件中有

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

xmlns:mvc="http://www.springframework.org/schema/mvc


时:

1.web.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "><!-- springmvc 扫包  @Controller @Service  .....--><context:component-scan base-package="com.sjzx" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>    <mvc:annotation-driven validator="validator" conversion-service="conversionService" />        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass"  value="org.hibernate.validator.HibernateValidator"/>       <!--  不设置则默认为classpath下的 ValidationMessages.properties -->        <property name="validationMessageSource" ref="validatemessageSource"/>    </bean>    <!-- 配置全局日期转换器由于  <mvc:annotation-driven 里面覆盖导致不能用 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >         <property name="converters">                <list>                    <bean class="com.sjzx.common.util.DateConverter" />                </list>            </property>      </bean>    <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">          <property name="basename" value="classpath:validatemessages.properties"/>          <property name="fileEncodings" value="utf-8"/>          <property name="cacheSeconds" value="120"/>  </bean> <!-- 配置全局日期转换器由于  <mvc:annotation-driven 里面覆盖导致不能用 --><!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="webBindingInitializer"><bean class="com.sjzx.common.util.CustomDateEdtor"/></property></bean> --><!-- jsp视图 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/back_page/"/><property name="suffix" value=".jsp"/></bean><!-- 上传图片 --><bean  id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 最大上传尺寸 --><property name="maxUploadSize" value="1048576"/></bean><!-- 映射器 -->  <mvc:interceptors>        <mvc:interceptor>       <!--默认拦截的连接-->            <mvc:mapping path="/control/*"/>            <!--不拦截的连接--><!--             <mvc:exclude-mapping path="/"/>  -->            <mvc:exclude-mapping path="/user/login"/>            <bean class="com.sjzx.common.interceptor.LoginInterceptor"></bean>        </mvc:interceptor>    </mvc:interceptors><!-- 适配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/></beans>
2.java
package com.sjzx.common.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.core.convert.converter.Converter;/** * 全局時間轉換器 * Title: DateConverter.java * Description: * @author xwp * @date 2017年4月26日上午9:36:16 * @version 1.0 */public class DateConverter implements Converter<String, Date> {@Overridepublic Date convert(String source) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");dateFormat.setLenient(false);try {return dateFormat.parse(source);} catch (ParseException e) {e.printStackTrace();}return null;}}


二.当配置文件中没有

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

xmlns:mvc="http://www.springframework.org/schema/mvc


时:

1.web.xml

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="webBindingInitializer"><bean class="com.sjzx.common.util.CustomDateEdtor"/></property></bean>


2.java
package com.sjzx.common.util;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.support.WebBindingInitializer;import org.springframework.web.context.request.WebRequest;/** * 初始化日期自定义转换器 Title: CustomDateEdtor.java Description: *  * @author xwp * @date 2017年3月15日下午5:13:08 * @version 1.0 */public class CustomDateEdtor implements WebBindingInitializer {public void initBinder(WebDataBinder binder, WebRequest request) {// 转换日期格式DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}@InitBinderpublic void initBinder(WebDataBinder binder) {DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");dateFormat.setLenient(true);binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}}







0 0