Spring MVC 의 요청 매개 변수 가 져 오기
1. @ PathVariabl 주 해 를 통 해 경로 에서 전달 하 는 매개 변 수 를 가 져 옵 니 다.
@RequestMapping(value = "/{id}/{str}")
public ModelAndView helloWorld(@PathVariable String id,
@PathVariable String str) {
System.out.println(id);
System.out.println(str);
return new ModelAndView("/helloWorld");
}
2. POST 가 요청 한 FORM 폼 데 이 터 를 @ ModelAttribute 주석 으로 가 져 오기
<form method="post" action="hao.do">
a: <input id="a" type="text" name="a" />
b: <input id="b" type="text" name="b" />
<input type="submit" value="Submit" />
</form>
JAVA pojo
public class Pojo{
private String a;
private int b;
....
3.
직접 사용
HttpServletRequest 획득
@RequestMapping(method = RequestMethod.GET)
public String get(HttpServletRequest request, HttpServletResponse response) {
System.out.println(request.getParameter("a"));
return "helloWorld";
}
4. 주석 @ RequestParam 바 인 딩 요청 매개 변수 a 에서 변수 a 로
요청 인자 a 가 존재 하지 않 을 때 이상 이 발생 할 수 있 습 니 다. 속성 required = false 를 설정 하여 해결 할 수 있 습 니 다. 예 를 들 어:
@RequestParam(value="a", required=false)
@RequestMapping(value = "/requestParam", method = RequestMethod.GET)
public String setupForm(@RequestParam("a") String a, ModelMap model) {
System.out.println(a);
return "helloWorld";
}