全局时间转换器

一.当配置文件中有

 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> {
	
	@Override
	public 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));
	}

	
	@InitBinder
	public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值