SpringMVC 사용자 정의 속성 편집기 상세 설명 및 인 스 턴 스

SpringMVC 사용자 정의 속성 편집기 상세 설명 및 인 스 턴 스
사용자 정의 springMVC 의 속성 편집 기 는 주로 두 가지 방식 이 있 습 니 다.하 나 는@InitBinder 탭 을 사용 하여 실행 기간 에 속성 편집 기 를 등록 하 는 것 입 니 다.이 편집 기 는 현재 Controller 에서 만 유효 합 니 다.또 하 나 는 자신의 WebBinding Initializer 를 실현 한 다음 AnnotationMethodHandlerAdapter 의 bean 을 정의 하 는 것 입 니 다.이 bean 에 등록 하 는 속성 편집 기 는 전역 적 입 니 다. 
첫 번 째 방법:

import java.beans.PropertyEditorSupport; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import javax.servlet.http.HttpServletResponse; 
 
import org.springframework.beans.propertyeditors.CustomDateEditor; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.annotation.InitBinder; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
 
@Controller 
public class GlobalController { 
 
   
  @RequestMapping("test/{date}") 
  public void test(@PathVariable Date date, HttpServletResponse response) throws IOException 
    response.getWriter().write( date); 
 
  } 
   
  @InitBinder//       WebDataBinder 
  public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); 
 
        binder.registerCustomEditor(int.class, new PropertyEditorSupport() { 
 
      @Override 
      public String getAsText() { 
        // TODO Auto-generated method stub 
        return getValue().toString(); 
      } 
 
      @Override 
      public void setAsText(String text) throws IllegalArgumentException { 
        // TODO Auto-generated method stub 
        System.out.println(text + "..........................................."); 
        setValue(Integer.parseInt(text)); 
      } 
       
    }); 
  } 
   
   
} 
  이런 식 으로 이렇게 쓰 면 돼 요.
 두 번 째: 
1.자신의 WebBinding Initializer 정의

package com.xxx.blog.util; 
 
import java.util.Date; 
import java.text.SimpleDateFormat; 
 
import org.springframework.beans.propertyeditors.CustomDateEditor; 
import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.support.WebBindingInitializer; 
import org.springframework.web.context.request.WebRequest; 
 
public class MyWebBindingInitializer implements WebBindingInitializer { 
 
  @Override 
  public void initBinder(WebDataBinder binder, WebRequest request) { 
    // TODO Auto-generated method stub 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); 
  } 
 
} 
 
2.springMVC 설정 파일 에 AnnotationMethodHandlerAdapter 를 정의 하고 WebBinding Initializer 속성 을 우리 가 정의 하 는 WebBinding Initializer 대상 으로 설정 합 니 다. 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="cacheSeconds" value="0"/> 
    <property name="webBindingInitializer"> 
      <bean class="com.xxx.blog.util.MyWebBindingInitializer"/> 
    </property> 
  </bean> 
 두 번 째 방식 은 위의 두 단 계 를 거 쳐 전역 속성 편집 기 를 정의 할 수 있 습 니 다.
메모:를 사용 하면 Default Annotation Handler Mapping 과 Annotation MethodHandler Adapter 두 개의 bean 을 자동 으로 등록 합 니 다.이 럴 때 두 번 째 방식 으로 지정 한 전역 속성 편집기 가 작 동 하지 않 습 니 다.해결 방법 은 위 bean 을 수 동 으로 추가 하고앞 에 추가 하 는 것 입 니 다.
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기