<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. 关于对vue-router的优化(详尽版)

    这两天总结了关于vue-router优化的几点技法,做个笔记 在基于vue的移动端app中,通过vue-router可以便捷的进入某一路由或回退到上一路由,但是若不对vue-router做相关优化处理 ...

  2. python学习13类2之封装

    '''''''''面向对象三大特性:封装,继承,多态1.封装: 类中以_或者__的属性,都是私有属性,禁止外部调用.'''class Student(object): def __init__(sel ...

  3. CentOS 7.4 安装网易云音乐

    1.下包–>网易云音乐 Ubuntu14.04(推荐14.04依赖包网上能找到) 提示:16.04有部分依赖包还找不到,有兴趣可以自行打包RPM安装. 2.解包 (1)使用 ar -vx解压ub ...

  4. 监控CPU与GPU的工具

    1.sensor:可以显示包括cpu在内的所有传感器的当前读数 使用sensors可以检测到cpu的温度,风扇的风速度,电压等. 2.Glances使用Python写的跨平台的curses的检测工具. ...

  5. C/C++ 程序执行时间

    C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock( void ); 这个函数返回从“开启这个程序进 ...

  6. 基于jenkins自动打包并部署Tomcat环境

    传统网站部署的流程 在运维过程中,网站部署是运维的工作之一.传统的网站部署的流程大致分为:需求分析->原型设计->开发代码->提交代码->内网部署->内网测试->确 ...

  7. 走 进 java 的 四 个 基 本 特 性

    赶上明天就还是五一c小长假了,准备在这几天写几篇原创文章,供大家一起学习. 首先今天就来好好地唠一唠,到底java的那几个特性都是什么呢?到底怎么用呢?相信一定有一些小白对此会有些懊恼,没关系的,谁还 ...

  8. 计算2的n次幂htm代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 【Linux常见命令】tail命令

    tail - output the last part of files tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件. tail -f filename  ...

  10. dhcpd.conf(5) - Linux man page

    http://linux.die.net/man/5/dhcpd.conf Name dhcpd.conf - dhcpd configuration file Description   The d ...