[Spring] @GetMapping | @PostMapping

Jeiniยท2023๋…„ 5์›” 21์ผ
0

๐Ÿƒย  Spring

๋ชฉ๋ก ๋ณด๊ธฐ
7/33
post-thumbnail

๐Ÿ’ก @GetMapping


โœ”๏ธ ์ฃผ์–ด์ง„ URI ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š” HTTP Get ์š”์ฒญ์„ ์ฒ˜๋ฆฌ.
โœ”๏ธ @GetMapping("/register/add")

โœ๏ธ @GetMapping ์˜ˆ์‹œ

@GetMapping("/register/add")
	public String register() {
		return "registerForm";
}
  • @RequestMapping(value="/register/add", method=RequestMethod.GET)
    : @RequestMapping ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ’ก @PostMapping


โœ”๏ธ ์ฃผ์–ด์ง„ URI ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š” HTTP POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌ.
โœ”๏ธ @PostMapping("/register/save")

โœ๏ธ @PostMapping ์˜ˆ์‹œ

@PostMapping("/register/save")
	public String register() {
		return "registerForm";
}
  • @RequestMapping(value="/register/save", method=RequestMethod.POST)
    : @RequestMapping ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ’ก @GetMapping | @PostMapping ํŠน์ง•


  • Spring 4.3๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  • url์„ ์ค‘๋ณต ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    : url ์š”์ฒญ์„ ํ†ตํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ์š”์ฒญํ•  ๋•Œ ์ „์†ก ๋ฐฉ์‹์„ ๋ช…์‹œํ•˜๋ฉด, ํ•˜๋‚˜์˜ url๋กœ๋„ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋งคํ•‘์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ฝ”๋“œ์— ์˜๋ฏธ ๋ช…์‹œ
    : ์ฝ”๋“œ๋งŒ ๋ด๋„ ์–ด๋–ค ์ „์†ก ๋ฐฉ์‹์„ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ํ†ต์ƒ์ ์œผ๋กœ GET์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ๋ฟŒ๋ฆด ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  POST๋Š” ์ „์†กํ•œ ๋ฐ์ดํ„ฐ๋ฅผ insertํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. ์ „์†ก ๋ฐฉ์‹์„ ๋ช…์‹œํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๋กœ์ง์„ ์ข€๋” ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธ ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ


โœ”๏ธ @GetMapping๊ณผ @PostMapping์„ ์ด์šฉํ•˜์—ฌ ํšŒ์›๊ฐ€์ž…์˜ˆ์ œ๋ฅผ ๋ง๋ถ™์—ฌ๋ณด์ž!

url๋กœ ํšŒ์›๊ฐ€์ž…์ด ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ˆ˜์ •ํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.
get๋ฐฉ์‹์œผ๋กœ ๋ณด๋‚ด์ง€๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ ์œ ์ถœ๋กœ ์ธํ•ด ๋ณด์•ˆ์ด ๋‚ฎ์•„์ง„๋‹ค. ๊ทธ๋ž˜์„œ post๋ฐฉ์‹์œผ๋กœ๋งŒ ๋  ์ˆ˜ ์žˆ๊ฒŒ ์ˆ˜์ •ํ•  ๊ฒƒ!

โœ๏ธ RegisterController.java ์ˆ˜์ • ์ „

package kr.ac.jipark09;

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

@Controller
public class RegisterController {
	// ์‹ ๊ทœ๊ฐ€์ž… ํ™”๋ฉด
	//@RequestMapping(value="/register/add", method= {RequestMethod.GET, RequestMethod.POST})
	@GetMapping("/register/add")
	public String register() {
		return "registerForm";
	}
	
	@PostMapping("/register/save")
	// ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
	//@RequestMapping(value="/register/save", method=RequestMethod.POST)
	public String save() {
		return "registerInfo";
	}
}
  • ์‚ฌ์‹ค ์ƒ, register() ๋ฉ”์„œ๋“œ๋Š” ๋งตํ•‘ํ•ด์„œ ์—ฐ๊ฒฐ๋งŒ ํ•  ๋ฟ์ด์ง€ ํ•˜๋Š” ์ผ์ด ์—†๋‹ค.
    โžก๏ธ ViewController ๋กœ ์ˆ˜์ •

โœ๏ธ servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<view-controller path="/register/add" view-name="registerForm"/>
	<context:component-scan base-package="kr.ac.jipark09" />
</beans:beans>
<view-controller path="/register/add" view-name="registerForm"/>
  • <view-controller> ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  • ์›๋ž˜ xmlns:mvc๋กœ ์ ‘๋‘์‚ฌ๋ฅผ ๋ถ™์—ฌ์•ผ๋˜์ง€๋งŒ, ํ•˜๋‚˜๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๊ทธ๋ž˜์„œ <view-controller> ๋„ ์›๋ž˜๋Š” <mvc:view-controller>์ด๋‹ค.
  • ์ƒ๋žต๋œ ์• ๋“ค์€ ๋‹ค mvc ์Šคํ‚ค๋งˆ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

โœ๏ธ ์‚ฌ์šฉ์ž ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ

โœ๏ธ registerForm.jsp

โœ”๏ธ $<param.msg> ์ถ”๊ฐ€
โœ”๏ธ dicoder ์ถ”๊ฐ€

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.net.URLDecoder"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" />
    <style>
        * { box-sizing:border-box; }

        form {
            width:400px;
            height:600px;
            display : flex;
            flex-direction: column;
            align-items:center;
            position : absolute;
            top:50%;
            left:50%;
            transform: translate(-50%, -50%) ;
            border: 1px solid rgb(89,117,196);
            border-radius: 10px;
        }

        .input-field {
            width: 300px;
            height: 40px;
            border : 1px solid rgb(89,117,196);
            border-radius:5px;
            padding: 0 10px;
            margin-bottom: 10px;
        }
        label {
            width:300px;
            height:30px;
            margin-top :4px;
        }

        button {
            background-color: rgb(89,117,196);
            color : white;
            width:300px;
            height:50px;
            font-size: 17px;
            border : none;
            border-radius: 5px;
            margin : 20px 0 30px 0;
        }

        .title {
            font-size : 50px;
            margin: 40px 0 30px 0;
        }

        .msg {
            height: 30px;
            text-align:center;
            font-size:16px;
            color:red;
            margin-bottom: 20px;
        }
        .sns-chk {
            margin-top : 5px; 
        }
    </style>
    <title>Register</title>
</head>
<body>
   <form action="<c:url value="/register/save"/>" method="post" onsubmit="return formCheck(this)">
    <div class="title">Register</div>
    <div id="msg" class="msg"> ${URLDecoder.decode(param.msg, "utf-8")}</div> 
    <label for="">์•„์ด๋””</label>
    <input class="input-field" type="text" name="id" placeholder="8~12์ž๋ฆฌ์˜ ์˜๋Œ€์†Œ๋ฌธ์ž์™€ ์ˆซ์ž ์กฐํ•ฉ">
    <label for="">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
    <input class="input-field" type="text" name="pwd" placeholder="8~12์ž๋ฆฌ์˜ ์˜๋Œ€์†Œ๋ฌธ์ž์™€ ์ˆซ์ž ์กฐํ•ฉ">
    <label for="">์ด๋ฆ„</label>
    <input class="input-field" type="text" name="name" placeholder="ํ™๊ธธ๋™">
    <label for="">์ด๋ฉ”์ผ</label>
    <input class="input-field" type="text" name="email" placeholder="example@fastcampus.co.kr"> 
    <label for="">์ƒ์ผ</label>
    <input class="input-field" type="text" name="birth" placeholder="2020/12/31">
    <div class="sns-chk">
        <label><input type="checkbox" name="sns" value="facebook"/>ํŽ˜์ด์Šค๋ถ</label>
        <label><input type="checkbox" name="sns" value="kakaotalk"/>์นด์นด์˜คํ†ก</label>
        <label><input type="checkbox" name="sns" value="instagram"/>์ธ์Šคํƒ€๊ทธ๋žจ</label>
    </div>
    <button>ํšŒ์› ๊ฐ€์ž…</button>
   </form> 
   <script>
       function formCheck(frm) {
            var msg ='';

            if(frm.id.value.length < 3) {
                setMessage('id์˜ ๊ธธ์ด๋Š” 3์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.', frm.id);
                return false;
            }
            if(frm.pwd.value.length < 3) {
            	setMessage('pw์˜ ๊ธธ์ด๋Š” 3์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.', frm.pwd);
                return false;
            }

           return true;
       }

       function setMessage(msg, element){
            document.getElementById("msg").innerHTML = `<i class="fa fa-exclamation-circle"> ${'${msg}'}</i>`;

            if(element) {
                element.select();
            }
       }
   </script>
</body>
</html>

โœ๏ธ 1. RegisterController.java

โœ”๏ธ URL์žฌ์ž‘์„ฑ(rewriting) ์‚ฌ์šฉ
โœ”๏ธ Incoding ์ถ”๊ฐ€

package kr.ac.jipark09;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RegisterController {
	
	@PostMapping("/register/save")
	// ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
	//@RequestMapping(value="/register/save", method=RequestMethod.POST)
	public String save(User user, Model model) throws UnsupportedEncodingException {
		// 1. ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
		if(!isValid(user)) {
			String msg = URLEncoder.encode("id๋ฅผ ์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.", "UTF-8");
			return "redirect:/register/add?msg=" + msg; //URL์žฌ์ž‘์„ฑ(rewriting): ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ธ์ฝ”๋”ฉ์„ ๋ชปํ•จ
		}
		 
		
		// 2. DB์— ์‹ ๊ทœํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅ
		return "registerInfo";
	}
	private boolean isValid(User user) {
		return false;
	}
}

โœ๏ธ 2. RegisterController.java

โœ”๏ธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด msg ํ•˜๋‚˜๋ผ์„œ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฐœ๋กœ ๋ณด๋‚ด์•ผ ๋  ๋•Œ์—๋Š” 1.์ฒ˜๋Ÿผ ํ•˜์ง€ ๋ชปํ•œ๋‹ค.
โžก๏ธ model์†์„ฑ ์‚ฌ์šฉ

package kr.ac.jipark09;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RegisterController {
	
	@PostMapping("/register/save")
	// ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
	//@RequestMapping(value="/register/save", method=RequestMethod.POST)
	public String save(User user, Model model) throws UnsupportedEncodingException {
		// 1. ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
		if(!isValid(user)) {
			String msg = URLEncoder.encode("id๋ฅผ ์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.", "UTF-8");
			
			model.addAttribute("msg", msg); // = "redirect:/register/add?msg=" + msg; -> ์Šคํ”„๋ง์ด ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์ค€๋‹ค.
			return "redirect:/register/add"; 
			
		}
		 
		
		// 2. DB์— ์‹ ๊ทœํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅ
		return "registerInfo";
	}
	private boolean isValid(User user) {
		return false;
	}
}

โœ๏ธ resgisterInfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>resgisterInfo</title>
</head>
<body>
<h1>id=${user.id}</h1>
<h1>pwd=${user.pwd}</h1>
<h1>name=${user.name}</h1>
<h1>email=${user.email}</h1>
<h1>birth=${user.birth}</h1>
<h1>sns=${user.sns}</h1>
</body>
</html>

Reference
: https://change-words.tistory.com/entry/Spring-%EC%8A%A4%ED%94%84%EB%A7%81-RequestMapping-%EB%8C%80%EC%8B%A0-PostMapping-GetMapping-%EC%93%B0%EB%8A%94-%EC%9D%B4%EC%9C%A0
: https://fastcampus.co.kr/dev_academy_nks

profile
Fill in my own colorful colors๐ŸŽจ

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