全局时间转换器
来源:互联网 发布:无间道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.javapackage 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
- 全局时间转换器
- 时间转换器
- 全局类型转换器
- (三)全局类型转换器
- Struts2_全局类型转换器
- struts2的转换器,局部,全局
- 第十四章 全局类型转换器
- struts2 自定义全局类型转换器
- javaEE配置全局日期转换器
- SpringMVC配置全局日期转换器
- springmvc配置全局日期转换器
- struts2自定义时间转换器
- python时间转换器
- vue 时间戳转换器
- Struts2实现自定义转换器和全局类型转换器
- Struts2实现自定义转换器和全局类型转换器
- Struts学习之局部类型转换器以及全局类型转换器
- Struts2之自定义局部类型转换器、全局类型转换器
- Javaweb开发入职第一天
- 解决使用小米手机调试android开发程序的问题
- 农村,是否值得你留恋?
- 总结jQuery的基础知识
- 方法重写Override笔记及其与Overload(重载)的区别
- 全局时间转换器
- Discovering Gold (期望,递推)
- github百度百科
- 使用Reachability的一个问题
- Grpc系列一 第一个hello world 例子
- 等和的分隔子集
- Angular2发布思路(整理官网Deployment页面)
- BZOJ 1146 [CTSC2008]网络管理Network
- Longest Substring Without Repeating Characters