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 ;}}