http://localhost:9090/request-param-v1?username=test&age=20
@RequestMapping("/request-param-v1") public void requestParamV1(HttpServletRequest request, HttpServletResponse response) throws IOException { String username = request.getParameter("username"); int age = Integer.parseInt(request.getParameter("age")); System.out.println("username : " + username); System.out.println("age : " + age); response.getWriter().write("ok"); }
@ResponseBody @RequestMapping("/request-param-v2") public String requestParamV2(@RequestParam("username") String username, @RequestParam("age") int age) throws IOException { return "username : " + username + "</br>age :" + age ; }
("변수이름")
생략가능username
=test3&age
=30username
, int age
@ResponseBody @RequestMapping("/request-param-v3") public String requestParamV3(@RequestParam String username, @RequestParam int age) throws IOException { return "username : " + username + "</br>age :" + age ; }
@ResponseBody @RequestMapping("/request-param-v4") public String requestParamV4(String username, int age) throws IOException { return "username : " + username + "</br>age :" + age ; }
@ResponseBody @RequestMapping("/request-param-required") public String requestParamRequired(@RequestParam(required = true) String username, @RequestParam(required = false) Integer age) throws IOException { return "username : " + username + "</br>age :" + age ; }
/request-param-required
-> 파라미터 안넘어 왔을 경우
-> username은 required = true로 필수이므로 에러발생!
/request-param-required?username=
-> 값만 안넘어 왔을 경우
->값이 넘어오지 않아도
,스프링 부트
가빈 문자열로 반환
하여에러는 발생하지 않는다.
/request-param-required?username=hello
-> age 파라미터가 없을 경우
-> required= false 이기 때문에 파라미터가 없어도 에러발생X
-> rapperclass인 Integer로 받기 때문에 null값으로 받을 수 있다.
@ResponseBody @RequestMapping("/request-param-defualt") public String requestParamDefault(@RequestParam(required = true, defaultValue = "guest") String username, @RequestParam(required = false, defaultValue = "-1") Integer age) throws IOException { return "username : " + username + "</br>age :" + age ; }
@ResponseBody @RequestMapping("/request-param-map") public String requestParamMap(@RequestParam Map<String, Object> paramMap) throws IOException { return "username : " + paramMap.get("username") + "</br>age :" + paramMap.get("age") ; }
@ModelAttribute 사용 전
@ResponseBody @RequestMapping("/model-attribute-v1") public String modelAttributeV1(@RequestParam String username, @RequestParam int age) { HelloData hello = new HelloData(); hello.setUsername(username); hello.setAge(age); return "username : " + hello.getUsername() + "</br>age :" + hello.getAge() ; }
@ModelAtrribute 사용
@ResponseBody @RequestMapping("/model-attribute-v2") public String modelAttributeV2(@ModelAttribute HelloData hellodata) { return "username : " + hellodata.getUsername() + "</br>age :" + hellodata.getAge() + "</br>toString : " + hellodata.toString() ; }
String, int와 같은 단순 타입
일 경우 스프링은 @ RequestParam 이 생략
되었다고 판단객체
일 경우 @ModelAttribute가 생략
되었다고 판단@ResponseBody @RequestMapping("/model-attribute-v3") public String modelAttributeV3(HelloData hellodata) { return "username : " + hellodata.getUsername() + "</br>age :" + hellodata.getAge() ; }
👌 @ModelAttribute가 메서드에 선언될 때
@ModelAttribute("regions") public Map<String, String> regions(){ Map<String, String> regions = new LinkedHashMap<String, String>(); regions.put("SEOUL", "서울"); regions.put("BUSAN", "부산"); regions.put("JEJU", "제주"); return regions; }