framework/spring mvc

[springMVC] 9. ์š”์ฒญ๋ฐฉ์‹์„ ์ง€์ •ํ•ด์„œ ์š”์ฒญ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ๋งŒ๋“œ๋Š”๋ฒ• @GetMapping @PostMapping

jeri 2024. 8. 4. 12:35
๋ฐ˜์‘ํ˜•
[๐Ÿ–คModel] MethodController
package xyz.itwill10.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MethodController {
  • ๐Ÿ’›์š”์ฒญ ๋ฐฉ์‹์— ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ : /method_input
	//์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ JSP ๋ฌธ์„œ(์ž…๋ ฅํŽ˜์ด์ง€)์˜ ViewName์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ
	// => ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋ฐฉ์‹(GET,POST,..)์— ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ๋˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	@RequestMapping("/method_input")
	public String inputOne() {
		return "method_input1";
	}
  • ๐Ÿ’›์š”์ฒญ ๋ฐฉ์‹์— ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ : /method_output
	// ์ž…๋ ฅ๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›์•„ Request Scope ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅํ•˜๊ณ  ์†์„ฑ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ JSP ๋ฌธ์„œ์˜ ViewName์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ
	// => ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋ฐฉ์‹(GET,POST,..)์— ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ๋˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
    
  	//=> request ๊ฐ์ฒด: request์—๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์œผ๋‹ˆ ์š”์ฒญ์— ํ•„์š”ํ•œ ์ •๋ณด ๋‹ฌ๋ผ๊ณ  ํ•˜๊ธฐ
  	//=> response ๊ฐ์ฒด: ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•  ๋•Œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ response์— ์ €์žฅํ—ค์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•˜๊ธฐ
	@RequestMapping("/method_output")
	public String outputOne(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ•ด๋‹น ํŽ˜์ด์ง€๋ฅผ GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ - ๋น„์ •์ƒ์ ์ธ ์š”์ฒญ
		if(request.getMethod().equals("GET")) {
			//response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); //ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฉ”์†Œ๋“œ - 405
            
      		//400๋Œ€ ์—๋Ÿฌ : ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
      		//401
      		//402
      		//403
      		//404 (๊ถŒํ•œ์ด ์—†์Œ)
      		//405 (์š”์ฒญ๋ฐฉ์‹์ด ์ž˜๋ชป๋จ)   ex. GET๋ฐฉ์‹ ์š”์ฒญ์ธ๋ฐ POST๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•  ๊ฒฝ์šฐ
            
			response.sendRedirect("method_input"); //ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ URL ์ฃผ์†Œ ์ „๋‹ฌ - URL ์ฃผ์†Œ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์žฌ์š”์ฒญ
			return null; //์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ ์•ˆํ•˜๋ฉด ์—๋Ÿฌ๋–จ์–ด์ง€๋ฏ€๋กœ null ๋ฐ˜ํ™˜ํ•ด์„œ ๋งŒ๋“ฆ
		}

		//POST๋ฐฉ์‹์˜ ์š”์ฒญ์ด๊ณ , JSP๋ฌธ์„œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ž…๋ ฅํŽ˜์ด์ง€(method_input1.jsp)์˜
    	//์บ๋ฆญํ„ฐ์…‹์ด utf-8์ด๋ฏ€๋กœ utf-8๋กœ ๋ณ€๊ฒฝํ•„์š”
		request.setCharacterEncoding("utf-8");
        
		String name = request.getParameter("name"); //์ „๋‹ฌ๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		request.setAttribute("name", name); //๋ทฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Request Scope์— ์ €์žฅ
        
		return "method_output";
	}
  • ๐Ÿ’š๊ฐœ์„  : [GET] ๋ฐฉ์‹์œผ๋กœ๋งŒ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ : /method
	//์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ JSP ๋ฌธ์„œ(์ž…๋ ฅํŽ˜์ด์ง€)์˜ ViewName์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ
	// => ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŽ˜์ด์ง€๋ฅผ [GET] ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ์—๋งŒ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	//method ์†์„ฑ : RequestMethod ์ž๋ฃŒํ˜•(Enum)์˜ ์ƒ์ˆ˜(Constant) ์ค‘ ํ•˜๋‚˜๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ •
	// => RequestMethod ์ž๋ฃŒํ˜•(Enum)์˜ ์ƒ์ˆ˜์—๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋ฐฉ์‹์„ ์ƒ์ˆ˜๋กœ ์ œ๊ณต
	// => method ์†์„ฑ์œผ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•œ ์š”์ฒญ๋ฐฉ์‹์„ ์ง€์ • ๊ฐ€๋Šฅ
	// => method ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ •๋œ ์š”์ฒญ๋ฐฉ์‹์ด ์•„๋‹Œ ๋ฐฉ๋ฒ•์œผ๋กœ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•œ ๊ฒฝ์šฐ 405 ์—๋Ÿฌ์ฝ”๋“œ ๋ฐœ์ƒ
	// => [GET] ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜ ๋Œ€์‹  @GetMapping ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
	@RequestMapping(value = "/method" , method = RequestMethod.GET)
	//@GetMapping("/method")
	public String inputTwo() {
		return "method_input2";
	}
  • ๐Ÿ’š๊ฐœ์„  : [POST] ๋ฐฉ์‹์œผ๋กœ๋งŒ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ : /method
	//์ž…๋ ฅ๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›์•„ Request Scope ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅํ•˜๊ณ  ์†์„ฑ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ JSP ๋ฌธ์„œ์˜ ViewName์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ
	// => ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŽ˜์ด์ง€๋ฅผ [POST] ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ์—๋งŒ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	// => ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์š”์ฒญ URL ์ฃผ์†Œ๊ฐ€ ๊ฐ™์•„๋„ ์š”์ฒญ๋ฐฉ์‹์„ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•˜๋ฉด ๋‹ค๋ฅธ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋กœ ๋งคํ•‘ ์ฒ˜๋ฆฌ
	// => [POST] ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜ ๋Œ€์‹  @PostMapping ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
	@RequestMapping(value = "/method" , method = RequestMethod.POST)
	//@PostMapping("/method")
	public String outputTwo(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		request.setAttribute("name", name);
		return "method_output";
	}
}
[๐Ÿ–คView] method_input1.jsp - ์ž…๋ ฅํŽ˜์ด์ง€
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>์ž…๋ ฅ ํŽ˜์ด์ง€</h1>
	<hr>
	<form action="/method_output" method="post">
		์ด๋ฆ„ : <input type="text" name="name">
		<button type="submit">์ „์†ก</button>
	</form>
</body>
</html>

[๐Ÿ–คView] method_input2.jsp - ์ž…๋ ฅํŽ˜์ด์ง€
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>์ž…๋ ฅ ํŽ˜์ด์ง€</h1>
	<hr>
	<form action="/method" method="post">
		์ด๋ฆ„ : <input type="text" name="name">
		<button type="submit">์ „์†ก</button>
	</form>
</body>
</html>

 

[๐Ÿ–คView] method_output.jsp - ์ถœ๋ ฅํŽ˜์ด์ง€
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>์ถœ๋ ฅ ํŽ˜์ด์ง€</h1>
	<hr>
	<h2>${name }๋‹˜, ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.</h2>
</body>
</html>

 

๋ฐ˜์‘ํ˜•