SPRINGMVC Customizing Global PropertyEditor

Source: Internet
Author: User

<mvc:annotation-driven></mvc:annotation-driven> injected @controller and @requestmapping required annotation classes

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/ >    <beanclass=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/>

When a custom global property converter (Jodatime) property Converter is required, annotations can be implemented. However, the actual project management, the use of annotation development, management is not good for the post-maintenance but the big hole ah, so personally feel that the use of configuration files for development more conducive to project maintenance.

1, first, in the latest version of Spring (4.1.6.RELEASE) version of the above two note classes are deprecated, instead of

Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

So not for <mvc:annotation-driven> but to maintain it yourself

2, the configuration file is as follows

<context:component-scan base-package= "com.cml.mvc.*"/><!--replace mvc:annotation-driven>-->< beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" ></bean ><!--uses SPRINGMVC's own JSON conversion tool to support @responsebody annotation--><beanclass= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/></list></ property><!--injected into the global 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><!--avoid IE when performing Ajax, return json appears download file--><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/jsonxml=application/xml</value></property ></bean>


3, the configuration file Webbindinginitializer we can inject the global PropertyEditor, detailed code:

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 (Base Webbindinginital.class);p rivate String timeformatter; @Overridepublic 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 is a custom 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 Date format Conversion * * April 22, 2015 */public class Jodatimepropertyeditor E Xtends PropertyEditorSupport {private static final log log = Logfactory.getlog (jodatimepropertyeditor.class);p rivate String formatter = "YyyyMMdd hhmmss";p rivate datetimeformatter format;public jodatimepropertyeditor (String formatter) { if (null! = Formatter) {this.formatter = formatter;} Format = Datetimeformat.forpattern (formatter);} @Overridepublic String Getastext () {Log.debug ("===>getastext:" + getValue ()); Object obj = GetValue (); if (null! = obj) { Return ((DateTime) obj). toString (formatter);} Return "";} @Overridepublic 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 Customizing Global PropertyEditor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.