SpringMVC 自定义全局PropertyEditor
来源:互联网 发布:淘宝开放平台 编辑:程序博客网 时间:2024/06/15 21:27
转载:http://blog.csdn.net/cml_blog/article/details/45222431
<mvc:annotation-driven></mvc:annotation-driven>注入了@Controller与@RequestMapping需要的注解类
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
当需要自定义全局属性转换器(JodaTime)属性转换器,可用注解实现。但是实际的项目管理中,使用注解开发,管理不好对于后期维护可是大坑啊,所以个人觉得还是使用配置文件进行开发更利于项目的维护。
1、首先,在spring最新版本(4.1.6.RELEASE)版本中上述的两个注解类已经不推荐使用了,取而代之的是
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapt
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
所以不适用<mvc:annotation-driven>而是自己来维护
2、配置文件如下
<context:component-scan base-package="com.cml.mvc.*" /> <!-- 取代mvc:annotation-driven> --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </bean> <!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 --> <bean class=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> <!-- 注入全局的propertiesEditor --> <property name="webBindingInitializer"> <bean class="com.cml.mvc.base.BaseWebBindingInital"> <property name="timeFormatter" value="yyyy-MM-dd HH:mm:ss"></property> </bean> </property> <property name="contentNegotiationManager" ref="contentNegotiationManager"></property> </bean> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="true" /> <property name="mediaTypes"> <value> json=application/json xml=application/xml </value> </property> </bean>
package com.cml.mvc.base; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.joda.time.DateTime; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest; import com.cml.mvc.property.editor.JodaTimePropertyEditor; public class BaseWebBindingInital implements WebBindingInitializer { private static final Log LOG = LogFactory .getLog(BaseWebBindingInital.class); private String timeFormatter; @Override public void initBinder(WebDataBinder binder, WebRequest request) { binder.registerCustomEditor(DateTime.class, new JodaTimePropertyEditor( timeFormatter)); LOG.debug("BaseWebBindingInital->initBinder=====>sessionId:" + Thread.currentThread().getId()); } public String getTimeFormatter() { return timeFormatter; } public void setTimeFormatter(String timeFormatter) { this.timeFormatter = timeFormatter; } }
4、JodaTimePropertyEditor是自定义的JodaTime的PropertyEditor
0 0
- SpringMVC 自定义全局PropertyEditor
- SpringMVC 自定义全局PropertyEditor
- Spring的自定义PropertyEditor
- 自定义SpringMVC全局参数转换
- spring配置自定义的PropertyEditor
- Spring IOC 自定义属性编辑器PropertyEditor
- Springmvc PropertyEditor实现绑定时的类型转换
- SpringMVC源码总结(八)类型转换PropertyEditor的背后
- Springmvc PropertyEditor实现绑定时的类型转换
- SpringMVC源码总结(八)类型转换PropertyEditor的背后
- Springmvc PropertyEditor实现绑定时的类型转换
- SpringMVC源码总结(八)类型转换PropertyEditor的背后
- Spring Bean包装类案例之自定义PropertyEditor
- Spring源代码分析(5)---propertyEditor(移花接木的自定义属性编辑器)
- 关于在Spring中注册自定义的PropertyEditor
- 关于在Spring中注册自定义的PropertyEditor
- springMVC 全局异常处理
- springMVC全局异常处理
- Centos7服务器安装nginx,mysql,php环境教程
- Jquery中Ajax/Post同步和异步请求
- ACM 利用位运算枚举所有子集
- 基于opencv和c++的均值滤波
- Noip2009解题报告
- SpringMVC 自定义全局PropertyEditor
- Intelli Idea全局替换
- JavaScript-说说为啥循环闭包只显示最后一个i的值
- 自定义视频播放器——VideoView和其他控件的组合
- ajax的data
- opencv实现车牌识别之车牌号定位_1
- gTimer protocol的实现
- servlet的自动加载+init方法+线程并发安全
- C++模板