โ๏ธ ์ฃผ์ด์ง URI ํํ์๊ณผ ์ผ์นํ๋ HTTP Get ์์ฒญ์ ์ฒ๋ฆฌ.
โ๏ธ @GetMapping("/register/add")
@GetMapping("/register/add")
public String register() {
return "registerForm";
}
โ๏ธ ์ฃผ์ด์ง URI ํํ์๊ณผ ์ผ์นํ๋ HTTP POST ์์ฒญ์ ์ฒ๋ฆฌ.
โ๏ธ @PostMapping("/register/save")
@PostMapping("/register/save")
public String register() {
return "registerForm";
}
Spring 4.3๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅ
url์ ์ค๋ณต ์ฌ์ฉ ๊ฐ๋ฅ
: url ์์ฒญ์ ํตํด ๋ฉ์๋๋ฅผ ์์ฒญํ ๋ ์ ์ก ๋ฐฉ์์ ๋ช
์ํ๋ฉด, ํ๋์ url๋ก๋ ๋ ๊ฐ ์ด์์ ๋งคํ์ ์ฒ๋ฆฌํ ์ ์๋ค.
์ฝ๋์ ์๋ฏธ ๋ช
์
: ์ฝ๋๋ง ๋ด๋ ์ด๋ค ์ ์ก ๋ฐฉ์์ ์ฒ๋ฆฌํ๋์ง ํ์ธํ ์ ์๋ ์ฅ์ ์ด ์๋ค. ํต์์ ์ผ๋ก GET์ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ๋ฟ๋ฆด ๋ ๋ง์ด ์ฌ์ฉ๋๊ณ POST๋ ์ ์กํ ๋ฐ์ดํฐ๋ฅผ insertํ ๋ ๋ง์ด ์ฌ์ฉ๋๋ค. ์ ์ก ๋ฐฉ์์ ๋ช
์ํจ์ผ๋ก์จ ์ฝ๋์ ๋ก์ง์ ์ข๋ ์ฝ๊ฒ ํ์
ํ ์ ์๋ค.
โ๏ธ @GetMapping๊ณผ @PostMapping์ ์ด์ฉํ์ฌ ํ์๊ฐ์ ์์ ๋ฅผ ๋ง๋ถ์ฌ๋ณด์!
url๋ก ํ์๊ฐ์
์ด ๋๋ ๊ฒ์ ๋ง๊ธฐ ์ํด ์์ ํด ๋ณผ ๊ฒ์ด๋ค.
get๋ฐฉ์์ผ๋ก ๋ณด๋ด์ง๋ฉด ๋น๋ฐ๋ฒํธ ์ ์ถ๋ก ์ธํด ๋ณด์์ด ๋ฎ์์ง๋ค. ๊ทธ๋์ post๋ฐฉ์์ผ๋ก๋ง ๋ ์ ์๊ฒ ์์ ํ ๊ฒ!
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
๋ก ์์ <?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 ์คํค๋ง๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
โ๏ธ $<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>
โ๏ธ 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;
}
}
โ๏ธ ํ๋ผ๋ฏธํฐ ๊ฐ์ด 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;
}
}
<%@ 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