[spring-mvc] 스프링 컨트롤러 매핑으로 URL 매개 변수를 어떻게 수신합니까?

이 문제는 사소한 것 같지만 제대로 작동하지 않습니다. jquery ajax로 Spring 컨트롤러 매핑을 호출하고 있습니다. someAttr의 값은 URL의 값에 관계없이 항상 빈 문자열입니다. 이유를 파악하도록 도와주세요.

-URL 호출

http://localhost:8080/sitename/controllerLevelMapping/1?someAttr=6

-컨트롤러 매핑

@RequestMapping(value={"/{someID}"}, method=RequestMethod.GET)
public @ResponseBody int getAttr(@PathVariable(value="someID") final String id, 
        @ModelAttribute(value="someAttr") String someAttr) {
    //I hit some code here but the value for the ModelAttribute 'someAttr' is empty string.  The value for id is correctly set to "1".
}



답변

@RequestParam대신을 사용해야합니다 @ModelAttribute. 예 :

@RequestMapping("/{someID}")
public @ResponseBody int getAttr(@PathVariable(value="someID") String id, 
                                 @RequestParam String someAttr) {
}

@RequestParam선택하는 경우 모두 생략 할 수도 있으며 Spring은 다음과 같이 가정합니다.

@RequestMapping("/{someID}")
public @ResponseBody int getAttr(@PathVariable(value="someID") String id, 
                                 String someAttr) {
}


답변

@RequestParam추가 옵션 요소와 함께 사용하기위한 많은 변형이 있습니다.

@RequestParam(required = false, defaultValue = "someValue", value="someAttr") String someAttr

넣지 않으면 required = false기본적으로 param이 필요합니다.

defaultValue = "someValue" -요청 매개 변수가 제공되지 않거나 값이 비어있는 경우 대체로 사용할 기본값입니다.

요청 및 메소드 매개 변수가 동일한 경우-필요하지 않습니다. value = "someAttr"


답변