2024. 7. 16. 20:13ใ๐ ๋ฐฑ์๋ ๊ฐ๋ฐ/SPRING
์ด๋ฒ์๋ MVC ํจํด์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค
MVC : Model, View, Controller ๋ก์จ, ์ ๊ธ์๋ฅผ ๋ฐ์์ MVC๋ผ๋ ๋จ์ด๊ฐ ์์ฑ์ด ๋์์ต๋๋ค
Spring์์๋ ๊ด์ฌ์ฌ๋ฅผ ๋ถ๋ฆฌํ๊ธฐ ์ํด์ MVCํจํด์ ์ฌ์ฉํฉ๋๋ค.
๊ทธ๋ผ ์ฝ๋๋ฅผ ์ง์ ์ง๋ณด๊ฒ ์ต๋๋ค!
package helloo.helloo_spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello") // get ์์ฒญ ์ฒ๋ฆฌ
public String hello(Model model) { // model ๊ฐ์ฒด ๋งค๊ฐ๋ณ์, ๋ฌธ์์ด ๋ฐํ
model.addAttribute("data", "hello!");
// model ๊ฐ์ฒด์ data ์์ฑ ์ถ๊ฐ, ๊ทธ ๊ฐ์ hello! ์ค์
return "hello"; // ๋ทฐ์ ์ด๋ฆ
// ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ํตํด์ ๋ทฐ ํ์ผ์ ์ฐพ๊ณ hello.html ๋ฐํ
}
@GetMapping("hello-mvc") // ์ฌ์ฉ์๊ฐ hello-mvc URL์ ์ ์ํ๋ฉด ๋ฉ์๋ ํธ์ถ
public String helloMvc(@RequestParam("name") String name, Model model) {
// @RequestParam ์ด๋
ธํ
์ด์
์ URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ค "name" ์ด๋ผ๋ ํค๋ฅผ ๊ฐ์ง๊ณ ์ค๊ฒ ๋ค ๋ผ๋ ์๋ฏธ
model.addAttribute("name", name);
return "hello-template";
}
}
// @RequestParam ์ถ๊ฐ์ ์ค๋ช
// ์ฌ์ฉ์๊ฐ http://localhost:8080/hello-mvc?name="spring" ์
๋ ฅ ํ ์ ์์
// @RequestParam("name")์์ name์ ์ฐพ์ ์ถ์ถ ํ name ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ
helloMvc ๋ฉ์๋๋ฅผ ๋ณด์๋ฉด ๋ ๊ฑฐ ๊ฐ์ต๋๋ค.
hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
์ฌ์ฉ์๊ฐ http://localhost:8080/hello-mvc?name="spring" ์ ์ ๋ ฅํ ์ ์ํ๋ฉด
์ด๋ ๊ฒ ๋จ๋๊ฑธ ๋ณผ ์ ์์ต๋๋ค
์ฌ๊ธฐ์ ์ถ๊ฐ์ ์ธ ์์ ์ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค
- ์ฌ์ฉ์๊ฐ index๋ฅผ ์ ๋ ฅํ๋ฉด ์๋์ ์ผ๋ก name ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํจ.
- ์ ๋ฌ๋ ์ธ์๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ํ์ด์ง
1. ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ์๋ ํผ์ ๋ง๋ค๊ฒ ์ต๋๋ค
templates/input.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ํผ ์
๋ ฅ ํ์ด์ง</title>
</head>
<body>
<h1>์ด๋ฆ์ ์
๋ ฅํ์ธ์</h1>
<form action="/hello-mvc-test" method="get">
<label for="name">์ด๋ฆ:</label>
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</form>
</body>
</html>
์ฌ๊ธฐ์ form action์ /hello-mvc-test๋ก ํ๊ธฐ ๋๋ฌธ์ submit์ ํ์๋ text์ ์ ํ name์ด /hello-mvc-test๋ก ์ ๋ฌ๋ฉ๋๋ค.
2. input.html์ ์ฒ๋ฆฌํ๊ธฐ ์ํด์ Controller๋ฅผ ๋ง๋ค๊ฒ ์ต๋๋ค
package helloo.helloo_spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello") // get ์์ฒญ ์ฒ๋ฆฌ
public String hello(Model model) { // model ๊ฐ์ฒด ๋งค๊ฐ๋ณ์, ๋ฌธ์์ด ๋ฐํ
model.addAttribute("data", "hello!");
// model ๊ฐ์ฒด์ data ์์ฑ ์ถ๊ฐ, ๊ทธ ๊ฐ์ hello! ์ค์
return "hello"; // ๋ทฐ์ ์ด๋ฆ
// ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ํตํด์ ๋ทฐ ํ์ผ์ ์ฐพ๊ณ hello.html ๋ฐํ
}
@GetMapping("hello-mvc") // ์ฌ์ฉ์๊ฐ hello-mvc URL์ ์ ์ํ๋ฉด ๋ฉ์๋ ํธ์ถ
public String helloMvc(@RequestParam("name") String name, Model model) {
// @RequestParam ์ด๋
ธํ
์ด์
์ URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ค "name" ์ด๋ผ๋ ํค๋ฅผ ๊ฐ์ง๊ณ ์ค๊ฒ ๋ค ๋ผ๋ ์๋ฏธ
model.addAttribute("name", name);
return "hello-template";
}
// ์ฌ๊ธฐ๋ถํฐ
@GetMapping("/")
public String input() {
return "input";
}
@GetMapping("hello-mvc-test")
public String helloMvcTest(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template-test";
}
// ์ฌ๊ธฐ๊น์ง
}
// @RequestParam ์ถ๊ฐ์ ์ค๋ช
// ์ฌ์ฉ์๊ฐ http://localhost:8080/hello-mvc?name="spring" ์
๋ ฅ ํ ์ ์์
// @RequestParam("name")์์ name์ ์ฐพ์ ์ถ์ถ ํ name ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ
์ด๋ฏธ static ํด๋์ index.html์ด ์๊ธฐ๋๋ฌธ์ GetMapping์ ์ํด์ฃผ๋ฉด static/index.html์ ๋ฐํํ๊ธฐ ๋๋ฌธ์
GetMapping์ ํตํด์ ๊ฒฝ๋ก๋ฅผ ๋ค์ ์ค์ ํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ์๊ฐ ์ ๋ ฅ ํผ์ ํตํด์ ์ ๋ ฅํ๊ณ ์ ์ถํ์๋ ๋ณด์ฌ์ฃผ๋ ํ์ด์ง ๊ฒฝ๋ก๋ฅผ ์ค์ ํ์ต๋๋ค.
3. ๊ฒฐ๊ณผ๊ฐ์ ๋ณด์ฌ์ฃผ๋ ํ์ด์ง
templates/hello-template-test.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ ์ถ๋ ฅ</title>
</head>
<body>
<h1>Hello, <span th:text="${name}">!</span></h1>
</body>
</html>
4. ๊ฒฐ๊ณผ
localhost:8080์ ๋ค์ด์ค๊ฒ ๋๋ฉด
์ด๋ฐ ํ์ด์ง๊ฐ ๋จ๊ณ , ์ด๋ฆ์ spring์ ์ ๋ ฅํ ํ submit์ ๋๋ฅด๋ฉด
์ด๋ฐ ํ๋ฉด์ด ๋จ๊ณ URL๋ http://localhost:8080/hello-mvc-test?name=spring
์ผ๋ก ๋ณ๊ฒฝ๋๊ฑธ ํ์ธ ํ ์ ์์ต๋๋ค
์คํ๊ณผ์
1. localhost:8080์ ๋ค์ด๊ฐ๊ฒ ๋๋ฉด ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ์๋ input.html์ ๋ณด์ฌ์ค
2. name์ ์ ๋ ฅํ๊ณ submit์ ๋๋ฅด๊ฒ ๋๋ฉด name๊ฐ์ด hello-mvc-test๋ก ์ ๋ฌ๋๋ค.
๊ทธ๋ ๊ฒ ๋๋ฉด http://localhost:8080/hello-mvc-test?name=์ ๋ ฅ๋๊ฐ ์ ๋ธ๋ผ์ฐ์ ๊ฐ ์์ฒญ์ ๋ณด๋ ๋๋ค.
3. hello-mvc-test ๊ฒฝ๋ก์ ๋ํ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ํ ์์ฒญ ํ๋ผ๋ฏธํฐ์์ name๊ฐ์ ๋ชจ๋ธ์ ์ถ๊ฐํ ํ
4. hello-template-test.html ๋ทฐ๊ฐ ๋ ๋๋ง๋๋ค.
์ ๊ธ์ ๊น์ํ๋์ spring ์ ๋ฌธ ๊ฐ์๋ฅผ ๋ฐํ์ผ๋ก ์ ์์ต๋๋ค!
'๐ ๋ฐฑ์๋ ๊ฐ๋ฐ > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ธํ๋ฐ ์๋ฐ์ ํด๋ฝ 2๊ธฐ - BE] 1์ฃผ์ฐจ ๋ฐ์๊ตญ (10) | 2024.10.06 |
---|---|
Spring ์๋ ์ฌ์์ (0) | 2024.07.25 |
Spring #4 ์ ์ ์ปจํ ์ธ (0) | 2024.07.16 |
Spring #3 View ํ๊ฒฝ์ค์ (0) | 2024.07.16 |
Spring #2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ! (0) | 2024.07.13 |