2009-08-12 64 views
22

我用在许多Spring的MVC控制器下面的自定义编辑器根据:如何在Spring-MVC中注册全局自定义编辑器?

控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

其他控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

另一个控制器

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

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

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

问候,

回答

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> 

详情here

+0

不它会覆盖默认的Spring PropertyEditors? – 2009-08-12 19:01:08

+0

是的。链接到上面的页面明确指出(表5.2。内置PropertyEditors) – ChssPly76 2009-08-12 19:03:08

+4

customEditors属性已被弃用,并且将在Spring 3中被删除(根据javadoc)。您应该改用PropertyEditorRegistrars属性。 – skaffman 2009-08-13 16:35:39

12

如果您使用基于注解控制器(春季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)); 
    } 

} 

东西,所以在你的web应用程序上下文文件,声明

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="GlobalBindingInitializer"/> 
    </property> 
</bean> 

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

30

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

如果您有相关的Spring Roo开始时生成的代码,或使用包括过滤器限制由组件扫描扫描的注释,然后添加所需的过滤器在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> 
相关问题