<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.RequestMappingHandlerAdapter
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>

3、配置文件中webBindingInitializer我们可以注入全局的PropertyEditor,详细代码:

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

package com.cml.mvc.property.editor;

import java.beans.PropertyEditorSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.util.StringUtils; /**
* jodaTime日期格式转换
* * 2015年4月22日
*/
public class JodaTimePropertyEditor extends PropertyEditorSupport { private static final Log LOG = LogFactory
.getLog(JodaTimePropertyEditor.class); private String formatter = "yyyyMMdd HHmmss";
private DateTimeFormatter format; public JodaTimePropertyEditor(String formatter) {
if (null != formatter) {
this.formatter = formatter;
}
format = DateTimeFormat.forPattern(formatter);
} @Override
public String getAsText() { LOG.debug("===>getAsText:" + getValue()); Object obj = getValue();
if (null != obj) {
return ((DateTime) obj).toString(formatter);
} return "";
} @Override
public void setAsText(String text) throws IllegalArgumentException { LOG.debug("datetime setAsText:" + text); if (StringUtils.isEmpty(text)) {
setValue(null);
} else { try {
setValue(format.parseDateTime(text));
} catch (Exception e) {
LOG.debug("format datetime error:" + e.getMessage() + ",value:"
+ text);
} } } public String getFormatter() {
return formatter;
} public void setFormatter(String formatter) {
this.formatter = formatter;
} }

SpringMVC 自定义全局PropertyEditor的更多相关文章

  1. springMVC自定义全局异常

    SpringMVC通过HandlerExceptionResolver处理程序异常,包括Handler映射,数据绑定以及目标方法执行时所发生的异常. SpringMVC中默认是没有加装载Handler ...

  2. SpringMVC 自定义全局日期转换器

    第一步: 编写自定义转换器的类 /* * 自定义日期转换器 */ public class CustomDateConverter implements Converter<String, Da ...

  3. 关于SpringMVC的全局异常处理器

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  4. 基于SpringMVC的全局异常处理器介绍(转)

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  5. SpringMVC实现全局异常处理器 (转)

    出处:  SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...

  6. SpringMVC 设置全局DateTime json返回格式

    对于部分返回DateTime的项目,只需要在指定属性上添加@JsonSerialize 使用自定义的json转换格式即可自定义返回DateTime格式 但是对于项目中返回有多个DateTime字段来说 ...

  7. 13.SpringMVC之全局异常

    我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生.在开发中,不管是dao层 ...

  8. mvc自定义全局异常处理

    异常信息处理是任何网站必不可少的一个环节,怎么有效显示,记录,传递异常信息又成为重中之重的问题.本篇将基于上篇介绍的html2cancas截图功能,实现mvc自定义全局异常处理.先看一下最终实现效果: ...

  9. Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效

    Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...

随机推荐

  1. es6最 全教程2020年

    带手机验证码登陆, 带全套购物车系统 带数据库 前后端分离开发 带定位用户功能 数据库代码为本地制作好了 带支付宝支付系统 带django开发服务器接口教程 地址:   https://www.dua ...

  2. C/C++,被誉为“最经典的编程语言”,不仅是因为编程入门需要学?

    计算机诞生初期,用机器语言或汇编语言编写程序; 第一种高级语言FORTRAN诞生于1954年; BASIC语言(1964)是由FORTRAN语言的简化而成的是为初学者设计的小型高级语言; C语言是19 ...

  3. input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。

    不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...

  4. Zabbix CPU utilization监控参数

    工作中查看Zabbix linux 监控项的时候对linux 监控的cpu使用的各个参数没怎么明白,特意查看了下资料 Zabbix linux模板下的CPU utilization是自带的监控Linu ...

  5. 13.Python中的命名空间是什么

    Python中的命名空间是什么? In Python,every name introduced has a place where it lives and can be hooked for. T ...

  6. VR全景视图 Google VrPanoramaView

    2019独角兽企业重金招聘Python工程师标准>>> 一.背景简介 Welcome to VR at Google 进入Google VR主页,发现官方给我们提供了两套解决观看VR ...

  7. 《Java 开发从入门到精通》—— 2.3 使用IDE工具序

    本节书摘来异步社区<Java 开发从入门到精通>一书中的第2章,第2.3节,作者: 扶松柏 , 陈小玉,更多章节内容可以访问云栖社区"异步社区"公众号查看. 2.3 使 ...

  8. 对包含HttpContext.Current.Cache的代码进行单元测试

    假设我们如下代码调用了HttpContext.Current.Cache 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class CacheManager { ...

  9. 2020最新nginx+gunicorn+supervisor部署基于flask开发的项目的生产环境的详细攻略

    本攻略基于ubuntu1804的版本,服务器用的华为云的服务器,python3(python2已经在2020彻底停止维护了,所以转到python3是必须的)欢迎加我的QQ6398903,或QQ群讨论相 ...

  10. postman(断言)

    一.断言 1.Code is 200 断言状态码是200 2.contains string 断言respoonse body中包含string 3.json value check (检查JSON值 ...