2009-08-12 24 views
22

tôi sử dụng trình biên tập tùy chỉnh sau trong NHIỀU điều khiển mùa xuân-MVC theo:Làm cách nào tôi có thể đăng ký trình chỉnh sửa tùy chỉnh chung trong Spring-MVC?

Một bộ điều khiển

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

điều khiển khác

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

điều khiển Một

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

Thông báo các cùng một biên tập viên tùy chỉnh đã đăng ký

Câu hỏi: làm cách nào tôi có thể thiết lập trình chỉnh sửa tùy chỉnh toàn cục như trình chỉnh sửa này để tránh thiết lập từng bộ điều khiển?

regards,

Trả lời

13

Bạn cần phải khai báo nó trong bối cảnh ứng dụng của bạn:

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

Cụ thể here

+0

Liệu nó ghi đè lên Spring PropertyEditors mặc định? –

+0

Có. Trang tôi liên kết ở trên nêu rõ rằng (Bảng 5.2. Trình biên dịch thuộc tính tích hợp) – ChssPly76

+4

Thuộc tính customEditors không được chấp nhận và sẽ bị xóa trong Mùa xuân 3 (theo javadoc). Bạn nên sử dụng thuộc tính PropertyEditorRegistrars để thay thế. – skaffman

12

Nếu bạn sử dụng một bộ điều khiển chú thích dựa (Spring 2.5+), bạn có thể sử dụng một WebBindingInitializer để đăng ký trình chỉnh sửa thuộc tính chung. Một cái gì đó như

public class GlobalBindingInitializer implements WebBindingInitializer { 

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

} 

Vì vậy, trong bối cảnh tập tin ứng dụng web của bạn, kê khai

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

Bằng cách này tất cả các bộ điều khiển chú thích dựa có thể sử dụng bất kỳ trình soạn tài sản kê khai trong GlobalBindingInitializer.

30

Bắt đầu từ mùa xuân 3.2, bạn có thể sử dụng @ControllerAdvice thay vì sử dụng @ExceptionHandler, @InitBinder và @ModelAttribute trong mỗi Bộ điều khiển. Chúng sẽ được áp dụng cho tất cả các bean @Controller.

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

Nếu bạn đã bắt đầu ra với Spring Roo tạo mã, hoặc hạn chế các chú thích được quét bởi thành phần quét sử dụng bao gồm bộ lọc, sau đó thêm các bộ lọc cần thiết trong 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> 
Các vấn đề liên quan