복붙노트

[SPRING] Spring MVC에서 전역 사용자 정의 편집기를 등록하려면 어떻게해야합니까?

SPRING

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

동일한 사용자 정의 편집기가 등록되어 있음을 확인하십시오.

질문 : 각 컨트롤러를 설치하지 않으려면 이처럼 글로벌 사용자 정의 편집기를 어떻게 설정할 수 있습니까?

문안 인사,

해결법

  1. ==============================

    1.애플리케이션 컨텍스트에서 선언해야합니다.

    애플리케이션 컨텍스트에서 선언해야합니다.

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

    자세한 내용은 여기에 있습니다.

  2. ==============================

    2.Spring 3.2부터는 @ Controller, @InitBinder, @ModelAttribute를 @ControllerAdvice로 사용할 수있다. 모든 @Controller 빈에 적용됩니다.

    Spring 3.2부터는 @ Controller, @InitBinder, @ModelAttribute를 @ControllerAdvice로 사용할 수있다. 모든 @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));
      }
    }
    

    Spring Roo에서 생성 된 코드로 시작했거나 include-filter를 사용하여 구성 요소 검사로 스캔 한 주석을 제한 한 경우 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>
    
  3. ==============================

    3.주석 기반 컨트롤러 (Spring 2.5 이상)를 사용하는 경우 WebBindingInitializer를 사용하여 전역 속성 편집기를 등록 할 수 있습니다. 좋아하는 것

    주석 기반 컨트롤러 (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));
        }
    
    }
    

    따라서 웹 응용 프로그램 컨텍스트 파일에서

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

    이 방법으로 모든 주석 기반 컨트롤러는 GlobalBindingInitializer에 선언 된 속성 편집기를 사용할 수 있습니다.

  4. from https://stackoverflow.com/questions/1268021/how-can-i-register-a-global-custom-editor-in-spring-mvc by cc-by-sa and MIT license