2024. 7. 16. 00:58ใ๐ ๋ฐฑ์๋ ๊ฐ๋ฐ/SPRING
์ด๋ฒ์ ํด๋ณผ๊ฒ์ ์ ์ ํ์ด์ง ๊ตฌ์ฑ๊ณผ welcomepage๋ฅผ ํ๋ฒ ๊ตฌ์ฑํด๋ณด๋๊ฑธ ํด๋ณผ๋ ค๊ณ ํ๋ค.
welcompage ๋ resources/static/index.html ์ ๊ตฌ์ฑํด์ฃผ๋ฉด Spring์์ ์๋์ ์ผ๋ก welcompage๋ฅผ ๋ง๋ค์ด์ค๋ค.
resources/static/index.html ํ์ผ์ ๋ง๋ค์ด์ฃผ๊ณ ๋์ ์ฝ๋๋ฅผ ์ฝ์ ํ๋ค.
<!DOCTYPE HTML>
<html>
<head>
<title>Hi!!!Spring</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello Spring!!
<a href="/hello">Hello Spring Link</a>
</body>
</html>
๋ง๋ค์ด์ฃผ๊ณ ๋์ Spring์ ๊ป๋ค ์ผ์ฃผ๋ฉด!!
์ด๋ ๊ฒ welcomepage๊ฐ ๊ตฌ์ถ์ด ๋์๋ค. welcomepage๋ ์ ์ ํ์ด์ง๋ก ์ฌ์ฉ์๊ฐ ์น์ฌ์ดํธ์ ์ ์์ ํ์๋
๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณต๋๋ ํ์ด์ง์ด๋ค.
๋ค์์ ์ ์ ํ์ด์ง๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ๋ค.
์ ์ฌ์ง์์ Hello Spring Link๋ผ๋ ๊ธ์จ๊ฐ aํ๊ทธ๋ก ๊ฐ์ธ์ ธ ์๋๋ฐ ๊ธ์จ๋ฅผ ๋๋ฅด๋ฉด /hello๋ก ํ์ด์ง๋ก ์ด๋ํ๋ ์ฝ๋๋ฅผ
๋ง๋ค์ด๋ณด๊ฒ ๋ค.
1. ์ฒซ๋ฒ์งธ๋ก๋ controller๋ฅผ ๋ง๋ค์ด์ค์ผ ํ๋ค.
- controller๋ ์ฌ์ฉ์๊ฐ ๋ค๋ฅธ ํ์ด์ง๋ก ์ด๋ํ ๋ ๊ฑฐ๊ธฐ์ ๋ง๋ ํ์ด์ง๋ฅผ Mapping ์์ผ์ฃผ๋ ์ญํ ์ ํ๋ค.
2. controller๋ผ๋ ํจํค์ง๋ฅผ ๋ง๋ค๊ณ HelloController class๋ฅผ ์์ฑํด์ค๋ค.
package helloo.helloo_spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello") // get ์์ฒญ ์ฒ๋ฆฌ
public String hello(Model model) { // model ๊ฐ์ฒด ๋งค๊ฐ๋ณ์, ๋ฌธ์์ด ๋ฐํ
model.addAttribute("data", "hello!");
// model ๊ฐ์ฒด์ data ์์ฑ ์ถ๊ฐ, ๊ทธ ๊ฐ์ hello! ์ค์
return "hello"; // ๋ทฐ์ ์ด๋ฆ
// ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ํตํด์ ๋ทฐ ํ์ผ์ ์ฐพ๊ณ hello.html ๋ฐํ
}
}
3. templates ํด๋์ hello.html ์์ฑ
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'์๋
ํ์ธ์. ' + ${data}" ></p>
</body>
</html>
HelloController hello ๋ฉ์๋์์ data ์ ๊ฐ์ด hello!๋ก ์ง์ ํ๊ธฐ ๋๋ฌธ์
<p th:text="'์๋
ํ์ธ์. ' + ${data}" ></p>
์ฌ๊ธฐ์ data ๊ฐ์ด hello! ๊ฐ ๋๋๊ฒ์ด๋ค.
์ ๊ธ์ ๊น์ํ๋์ Spring ์ ๋ฌธ ๊ฐ์ ๋ฐํ์ผ๋ก ๊ธ์ ์ผ์ต๋๋ค!
'๐ ๋ฐฑ์๋ ๊ฐ๋ฐ > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring ์๋ ์ฌ์์ (0) | 2024.07.25 |
---|---|
Spring #5 MVC์ ํ ํ๋ฆฟ ์์ง (0) | 2024.07.16 |
Spring #4 ์ ์ ์ปจํ ์ธ (0) | 2024.07.16 |
Spring #2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ! (0) | 2024.07.13 |
Spring #1 Spring ํ๋ก์ ํธ ์์ฑ (0) | 2024.07.13 |