如何在Spring-MVC中注册全局自定义编辑器?

Art*_*ald 24 data-binding spring spring-mvc

我根据以下内容在MANY Spring-MVC控制器中使用以下自定义编辑器:

一个控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)

其他控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)

另一个控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)

注意注册了相同的自定义编辑器

问题:如何设置像这样的全局自定义编辑器以避免设置每个控制器?

问候,

kri*_*arp 30

从Spring 3.2开始,您可以使用@ControllerAdvice而不是在每个Controller中使用@ ExceptionHandler,@ InitBinder和@ModelAttribute.它们将应用于所有@Controller bean.

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public class GlobalBindingInitializer {
  @InitBinder
  public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
    binder.registerCustomEditor(BigDecimal.class, new  CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您已经开始使用Spring Roo生成的代码,或者使用include-filter限制component-scan扫描的注释,那么在webmvc-config.xml中添加所需的过滤器

<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
<context:component-scan base-package="com.sensei.encore.maininterface" use-default-filters="false">
  <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
  <!-- ADD THE BELOW LINE -->
  <context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)


Chs*_*y76 13

您需要在应用程序上下文中声明它:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors"><map>
    <entry key="java.math.BigDecimal">
      <bean class="org.springframework.beans.propertyeditors.CustomNumberEditor">
      ... <!-- specify constructor-args here -->
      </bean>
    </entry>
  </map></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

细节在这里

  • 不推荐使用customEditors属性,将在Spring 3中删除(根据javadoc).您应该使用PropertyEditorRegistrars属性. (4认同)

Art*_*ald 13

如果使用基于注释的控制器(Spring 2.5+),则可以使用WebBindingInitializer注册全局属性编辑器.就像是

public class GlobalBindingInitializer implements WebBindingInitializer {

    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
    }

}
Run Code Online (Sandbox Code Playgroud)

因此,在您的Web应用程序上下文文件中,声明

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="GlobalBindingInitializer"/>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

这样,所有基于注释的控制器都可以使用GlobalBindingInitializer中声明的任何属性编辑器.