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>
두 번 째 방식 은 위의 두 단 계 를 거 쳐 전역 속성 편집 기 를 정의 할 수 있 습 니 다.메모:
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ssm 프레임워크 업로드 이미지 로컬 및 데이터베이스에 저장 예시본고는 ssm 프레임워크 업로드 이미지를 로컬과 데이터베이스에 저장하는 예시를 소개하고 주로 Spring+SpringMVC+MyBatis 프레임워크를 사용하여 ssm 프레임워크 업로드 이미지의 실례를 실현했다. 구체...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.