[Spring_Boot] RequestParam & ResponseParam/ ResponseBody/ ModelAndView

์ตœํ˜„์„ยท2022๋…„ 11์›” 23์ผ
0

Spring_Boot

๋ชฉ๋ก ๋ณด๊ธฐ
8/31

๐Ÿงฉ RequestParam , ResponseParam

  • @ReuqestParam ์–ด๋…ธํ…Œ์ด์…˜์€ HttpServletRequest ๊ฐ์ฒด์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.
  • HttpServletRequest์—์„œ๋Š” getParameter() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ–ˆ์ง€๋งŒ, @RequestParam์„ ์ด์šฉํ•ด์„œ ๋ฐ›์•„์˜ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.
  • ๋ฉ”์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์œผ๋กœ @RequestParam์„ ๋„ฃ์–ด์ฃผ๋ฉด๋œ๋‹ค.

๐Ÿงฉ์‹ค์Šต

1) hello-form

<body>
	<form action="/request-param-v1" method="post">
		username : <input type="text" name="username"/><br>
		age		 : <input type="text" name="age">
		<button type="submit">์ „์†ก</button>
	</form>
</body>

2) HelloData

  • lombok jar ํŒŒ์ผ ์‚ฌ์šฉ
  • Getter Setter ToString๋ฅผ @Getter @Setter @ToString ๋Œ€์ฒด
@Getter @Setter @ToString
public class HelloData {
	
	private String username;
	private int age;
}

3) RequestParamController

  • @RequestParam ์žˆ๋Š” ๊ฒฝ์šฐ required=true ๊ธฐ๋ณธ
  • @RequestParam ์—†๋Š” ๊ฒฝ์šฐ required=false ๊ธฐ๋ณธ

3-1) @ResponseBody

  • @Controller ์–ด๋…ธํ…Œ์ด์…˜์ด ์ž‘์„ฑ๋œ ์ž๋ฐ”ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ ์ž ํ• ๋•Œ๋Š” @ResponseBody๋ฅผ ์‚ฌ์šฉ
  • view ์กฐํšŒ๋ฅผ ๋ฌด์‹œํ•˜๊ณ , HTTP message body์— ์ง์ ‘ ํ•ด๋‹น ๋‚˜์šฉ ์ž…๋ ฅ
  • response.getWriter().write("ok"); -> @ResponseBody
    ๋‹ด๊ณ ์ž ํ•˜๋Š” ๋ฉ”์„ธ์ง€๋ฅผ String ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค

3-2) @RequestParam

  • ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๋ฐ”์ธ๋”ฉ
  • name์†์„ฑ์ด ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ

3-3) defaultValue

  • ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ defaultValue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ ๊ฐ’์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— required๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค
  • ๋นˆ ๋ฌธ์ž์—ด์—๋„ ์ ์šฉ
@Controller
public class RequestParamController{

	@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);
		// void ํƒ€์ž… -> ViewResolver์— ์•ˆ๋ณด๋‚ธ๋‹ค
		// ํ™”๋ฉด์ด ์•„๋‹Œ ๊ฒฐ๊ณผ ๊ฐ’๋งŒ์„ ๋ฆฌํ„ด, return ์—†์ด ์‚ฌ์šฉ
		response.getWriter().write("ok");
	}
	
	/*
	 * @ResponseBody
	 *  - view ์กฐํšŒ๋ฅผ ๋ฌด์‹œํ•˜๊ณ , HTTP message body์— ์ง์ ‘ ํ•ด๋‹น ๋‚˜์šฉ ์ž…๋ ฅ
     *  - response.getWriter().write("ok"); -> @ResponseBody
	 *   ๋‹ด๊ณ ์ž ํ•˜๋Š” ๋ฉ”์„ธ์ง€๋ฅผ String ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค
	 *  ------------------------------
	 *  @RequestParam
	 *   - ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๋ฐ”์ธ๋”ฉ
	 *   - name์†์„ฑ์ด ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ
	 *   -----------------------------
	 *   http://localhost:9090/request-param-v2?username=test&age=20
     *	 ?username = @RequestParam("username")  ๋งคํ•‘
     *   String username = (+ username);  ๋งคํ•‘
	 */
	@ResponseBody
	@RequestMapping("/request-param-v2")
	public String requestParamV2(@RequestParam("username") String username,
			@RequestParam("age") int age) 
			throws IOException {
		
		System.out.println("username : "+ username);
		System.out.println("age : "+ age);
		return "ok";
	}
	
	/*
	 * @RequestParam ์‚ฌ์šฉ
	 *  - HTTP ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์ด ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ๊ฐ™์œผ๋ฉด
	 *  @RequestParam("๋ณ€์ˆ˜์ด๋ฆ„") ์ƒ๋žต ๊ฐ€๋Šฅ
	 *  -------------------------------
	 *  http://localhost:9090/request-param-v3?username=test&age=20
	 */
	@ResponseBody
	@RequestMapping("/request-param-v3")
	public String requestParamV3(@RequestParam String username,@RequestParam int age) 
			throws IOException {
		
		System.out.println("username : "+ username);
		System.out.println("age : "+ age);
		return "ok";
	}
	
	/*
	 * @RequestParam
	 *  - String, int ๋“ฑ ๋‹จ์ˆœ ํƒ€์ž…์ด๋ฉด @RequestParam ์ƒ๋žต ๊ฐ€๋Šฅ
	 *  - MVC๋‚ด๋ถ€์—์„œ required=false ๋ฅผ ์ ์šฉํ•œ๋‹ค.
	 *  - ๋น„์ถ”!!
	 */
	@ResponseBody
	@RequestMapping("/request-param-v4")
	public String requestParamV4( String username, int age) throws IOException {
		
		System.out.println("username : "+ username);
		System.out.println("age : "+ age);
		return "ok";
	}
	
	/*
	 * required=true : ๋ฐ˜๋“œ์‹œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ๋“ค์–ด์™€์•ผํ•œ๋‹ค.
     * required=false : null
	 * -----------------------------------
	 * @RequestParam -> required
	 * /request-param-required 				-> username์ด ์—†์„๊ฒฝ์šฐ ์—๋Ÿฌ
	 * /request-param-required?username= 	-> ๋นˆ ๋ฌธ์ž๋กœ ํ†ต๊ณผ
	 * /request-param-required?username=test
	 * 		-> null์„ int์— ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅ, ๋”ฐ๋ผ์„œ Integer๋กœ ๋ณ€๊ฒฝํ•ด์•ผํ•จ
	 */
	@ResponseBody
	@RequestMapping("/request-param-required")
	public String requestParamRequired(@RequestParam(required=true) String username,
			@RequestParam(required=false) Integer age) throws IOException {
		
		System.out.println("username : "+ username);
		System.out.println("age : "+ age);
		return "ok";
	}
	
	/*
	 * defaultValue
	 *  - ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ defaultValue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ ๊ฐ’์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
	 *  - ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— required๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค
	 *  - ๋นˆ ๋ฌธ์ž์—ด์—๋„ ์ ์šฉ 
	 */
	@ResponseBody
	@RequestMapping("/request-param-default")
	public String requestParamDefault(
			@RequestParam(required=true, defaultValue = "guest") String username,
			@RequestParam(required=false, defaultValue = "-1") Integer age) 
			throws IOException {
		
		System.out.println("username : "+ username);
		System.out.println("age : "+ age);
		return "ok";
	}
	
	/*
	 * @RequestParam
	 *  - Map์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ
     *   Map<String, Object> ํ‚ค๊ฐ’,ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’
	 */
	@ResponseBody
	@RequestMapping("/request-param-map")
	public String requestParamMap(@RequestParam Map<String, Object> paramMap )
			throws IOException {
		
		System.out.println("username : "+ paramMap.get("username"));
		System.out.println("age : "+ paramMap.get("age"));
		
		return "ok";
	}
	
    /*
    * HelloData.java
	*/
	@ResponseBody
	@RequestMapping("/model-attribute-v1")
	public String modelAttributeV1(@RequestParam String username, 
			@RequestParam int age) {
		
		HelloData hello = new HelloData();
		hello.setUsername(username);
		hello.setAge(age);
		System.out.println("username : " + hello.getUsername());
		System.out.println("age : " + hello.getAge());
		
		return "ok";
	}
	
	
	/*
	 * @ModelAttribute
	 *  - ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ๊ฐ์ฒด์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •์„ ์ž๋™ํ™”
	 */
	@ResponseBody
	@RequestMapping("/model-attribute-v2")
	public String modelAttributeV2(@ModelAttribute HelloData helloData) {
		
		System.out.println("username : " + helloData.getUsername());
		System.out.println("age : " + helloData.getAge());
		System.out.println("HelloData : " + helloData.toString());
		
		return "ok";
	}
	
	/*
	 * @ModelAttribute ์ƒ๋žต๊ฐ€๋Šฅ
	 *  - String, int ๊ฐ™์€ ๋‹จ์ˆœ ํƒ€์ž… -> @RequestParam
	 *  - ๊ฐ์ฒด -> @ModelAttribute
	 */
	@ResponseBody
	@RequestMapping("/model-attribute-v3")
	public String modelAttributeV3( HelloData helloData) {
		
		System.out.println("username : " + helloData.getUsername());
		System.out.println("age : " + helloData.getAge());
		System.out.println("HelloData : " + helloData.toString());
		
		return "ok";
	}
}

4) hello

<body>
	<p th:text="${data}">empty</p>
	
</body>

5) ResponseViewController

5-1) ModelAndView

  • Controller ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ํ›„ ์‘๋‹ตํ•  view์™€ view์— ์ „๋‹ฌํ•  ๊ฐ’์„ ์ €์žฅ
@Controller
public class ResponseViewController {
	
	/*
	 * ModelAndView
	 *  - Controller ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ํ›„ ์‘๋‹ตํ•  view์™€ view์— ์ „๋‹ฌํ•  ๊ฐ’์„ ์ €์žฅ
	 */
	@RequestMapping("/response-view-v1")
	public ModelAndView responseViewV1() {
		ModelAndView mav = new ModelAndView("response/hello").addObject("data","hello!");
		return mav;
	}
	
	/*
	 * @Controller ์—์„œ return์ด String ์ด๋ฉด view์˜ ๋…ผ๋ฆฌ์ ์ธ ์ด๋ฆ„์ด ๋œ๋‹ค.
	 * @ResponseBody๋ฅผ ๋„ฃ์ง€ ์•Š๋„๋ก ์ฃผ์˜
     * 	๋„ฃ๋Š”๋‹ค๋ฉด viewresolver์— ํ™”๋ฉด์„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๋Š”๋‹ค,
	 *  return์— ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ๊ฐ€ ํ™”๋ฉด์— ์ถœ๋ ฅ
	 */
     //	@ResponseBody
	@RequestMapping("/response-view-v2")
	public String responseViewV2(Model model) {
		model.addAttribute("data","model data");
		return "response/hello";
	}
}	

0๊ฐœ์˜ ๋Œ“๊ธ€