SpringMVC custom global PropertyEditor

Source: Internet
Author: User

SpringMVC custom global PropertyEditor

Annotation classes required by @ Controller and @ RequestMapping are injected.

 

    
     
  
 

 

You can use annotations to customize the JodaTime attribute converter. However, in actual project management, it is difficult to use annotations for development and management. Therefore, I personally feel that using configuration files for development is more conducive to project maintenance.

1. First, the above two annotation classes are not recommended in the latest Spring version (4.1.6.RELEASE). Instead

 

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

So not applicable It is maintained by yourself.

2. The configuration file is as follows:

 

 
    
  
  
   
    
     
      text/html;charset=UTF-8
     
    
    
  
  
   
    
     
      
       json=application/jsonxml=application/xml
      
     
    
   
  
 

 

 

3. In the configuration file, we can inject the global PropertyEditor into webBindingInitializer. The detailed code is as follows:

 

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;@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 the custom JodaTime PropertyEditor.

 

 

Package com.. 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 exten Ds 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) ;}@ 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 ;}}




 



 

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.