[SPRING] 봄에 경로 변수를 사용자 정의 모델 객체에 바인딩
SPRING봄에 경로 변수를 사용자 정의 모델 객체에 바인딩
내 요청을 모델링하는 클래스가 있습니다.
class Venue {
private String city;
private string place;
// Respective getters and setters.
}
그리고 RESTful URL을 지원하여 장소에 대한 정보를 얻고 싶습니다. 그래서 나는 이런 컨트롤러 방법을 가지고 있습니다.
@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(@PathVariable("city") String city, @PathVariable("place") String place, Model model) {
// code
}
방법이 있습니까? 봄에 모든 개별 매개 변수를 가져 오는 대신 모델 객체 (이 경우 Venue)에 경로 변수를 바인딩 할 수 있습니까?
해결법
-
==============================
1.Spring MVC는 요청 매개 변수와 경로 변수를 JavaBean에 바인딩 할 수있는 기능을 제공한다.이 경우 JavaBean은 Venue이다. 예 :
Spring MVC는 요청 매개 변수와 경로 변수를 JavaBean에 바인딩 할 수있는 기능을 제공한다.이 경우 JavaBean은 Venue이다. 예 :
@RequestMapping(value = "/venue/{city}/{place}", method = "GET") public String getVenueDetails(Venue venue, Model model) { // venue object will be automatically populated with city and place }
JavaBean에는 도시 및 장소 속성이 있어야 작동한다는 점에 유의하십시오.
자세한 내용은 spring-projects / spring-mvc-showcase의 withParamGroup () 예제를 참조하십시오.
-
==============================
2.http://static.springsource.org/spring/docs/3.2.3.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates에있는 Spring 설명서에 따라 자동 단순한 유형에만 지원이 제공됩니다.
http://static.springsource.org/spring/docs/3.2.3.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates에있는 Spring 설명서에 따라 자동 단순한 유형에만 지원이 제공됩니다.
@RequestParam과 Model의 특정 조합을 시도하지는 않았지만, http://static.springsource.org/spring/docs/3.2.3에서 자세하게 설명 된대로 사용자 정의 WebBindingInitializer를 작성하여 원하는 구현을 구현할 수있는 것처럼 보입니다. .RELEASE / spring-framework-reference / html / mvc.html # mvc-ann-typeconversion.
사용자 정의 클래스는 WebRequest에 대한 액세스 권한을 가지며이 요청에서 추출한 데이터로 채워진 도메인 객체를 반환합니다.
-
==============================
3.HandlerMethodArgumentResolver 인터페이스의 구현을 제공 할 수 있습니다. 이 인터페이스는 주로 Spring이 할 수없는 방식으로 컨트롤러 메소드에 대한 인수를 해결하는 데 사용됩니다. 이 두 가지 방법을 구현하여이 작업을 수행 할 수 있습니다.
HandlerMethodArgumentResolver 인터페이스의 구현을 제공 할 수 있습니다. 이 인터페이스는 주로 Spring이 할 수없는 방식으로 컨트롤러 메소드에 대한 인수를 해결하는 데 사용됩니다. 이 두 가지 방법을 구현하여이 작업을 수행 할 수 있습니다.
public boolean supportsParameter(MethodParameter mp) { ... } public Object resolveArgument(mp, mavc, nwr, wdbf) throws Exception { ... }
다음을 통해 구현을 Spring 컨텍스트에 삽입 할 수 있습니다.
<mvc:annotation-driven> <mvc:argument-resolvers> <bean class="com.path.ImplementationOfHandlerMethodArgumentResolver"/> </mvc:argument-resolvers> </mvc:annotation-driven>
Spring이 해결할 수없는 매개 변수를 발견하면, 당신의 메소드 supportsParameter ()를 호출하여 해석자가 매개 변수를 해석 할 수 있는지 확인합니다. 메소드가 true를 반환하면 Spring은 resolveArgument () 메서드를 호출하여 실제로 매개 변수를 해석합니다. 이 메서드에서는 NativeWebRequest 객체에 액세스 할 수 있습니다.이 객체를 사용하여 contextPath를 넘어 경로를 가져올 수 있습니다. (귀하의 경우 : / 장소 / {도시} / {장소}) 요청 경로를 통해 구문 분석하고 도시 / 장소 문자열을 가져 오려고 시도 할 수 있습니다. Venue 객체에 채 웁니다.
from https://stackoverflow.com/questions/17149425/bind-path-variables-to-a-custom-model-object-in-spring by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 하나의 클래스 만 사용하여 @ComponentScan 또는 <context : component-scan /> 사용 (0) | 2019.01.18 |
---|---|
[SPRING] Java 7에서 AspectJ AOP를 사용할 때의 오류 (0) | 2019.01.18 |
[SPRING] 스프링과 비정상적인 아키텍처의 엔티티 관리자에 대한 정적 액세스 (0) | 2019.01.18 |
[SPRING] 동적 데이터 소스로 Spring AbstractRoutingDataSource를 사용하는 방법? (0) | 2019.01.18 |
[SPRING] Spring 관리 빈에서 @ManagedProperty가 null입니다. (0) | 2019.01.18 |