Spring mvc 在Spring MVC 3.0中是否有一种基于注释的方法全局注册PropertyEditor?
我想知道是否有一种方法可以在SpringMVC3.0以后的版本中全局注册PropertyEditor。在文档中,他们展示了如何使用注释在每个控制器的基础上定制bean PropertyEditor,在我看来,这就像是一种在全局范围内实现的XML方式。所以我想知道,有没有一种方法,只使用注释为所有控制器注册PropertyEditor,而不必为每个控制器执行@InitBinder方法。用@InitBinder方法创建一个公共的超类也是不可取的 在Spring3.0发布之前,就这一主题提出了一些问题Spring mvc 在Spring MVC 3.0中是否有一种基于注释的方法全局注册PropertyEditor?,spring-mvc,annotations,javabeans,propertyeditor,Spring Mvc,Annotations,Javabeans,Propertyeditor,我想知道是否有一种方法可以在SpringMVC3.0以后的版本中全局注册PropertyEditor。在文档中,他们展示了如何使用注释在每个控制器的基础上定制bean PropertyEditor,在我看来,这就像是一种在全局范围内实现的XML方式。所以我想知道,有没有一种方法,只使用注释为所有控制器注册PropertyEditor,而不必为每个控制器执行@InitBinder方法。用@InitBinder方法创建一个公共的超类也是不可取的 在Spring3.0发布之前,就这一主题提出了一些问题
package com.projectr.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
/**
* Shared WebBindingInitializer for custom property editors.
*
* @author aramirez
*
*/
public class CommonBindingInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}
}
在应用程序上下文或调度程序servlet中
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.projectr.web.CommonBindingInitializer"/>
</property>
</bean>
考虑到你的例子根本没有使用注释,我不明白这是如何回答这个问题的。我很抱歉。上述xml代码的注释等价于@InitBinder。但我想你已经知道了,因为我的回复太晚了。不记得我有没有想过。想发布一个例子作为补充答案,我会接受吗\
@ControllerAdvice
public class CommonBindingInitializer {
@InitBinder
public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}
}