Question

package com.mysite.sbb.question;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class QuestionController {
@GetMapping("/question/list")
public String list() {
return "question_list";
}
}
์๋ฒ ์คํํ๊ธฐ

view๊ฐ ์์ผ๋๊น ๋น์ฐํ ์๋ฌ๊ฐ ๋์ค๋๊ฒ ๋ง์
ํ ํ๋ฆฟ ์ ๋ฃ๊ณ jsp๋ฅผ ์์
ํ ํ๋ฆฟ ์ค์ ํ๊ธฐ
์ผ๋ฐ์ ์ผ๋ก ๋ง์ด ์ฌ์ฉํ๋ ๋ฐฉ์์ ํ ํ๋ฆฟ ๋ฐฉ์์ด๋ค. ํ ํ๋ฆฟ์ ์๋ฐ ์ฝ๋๋ฅผ ์ฝ์ ํ ์ ์๋ HTML ํ์์ ํ์ผ์ด๋ค.
ํ ํ๋ฆฟ์ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์๋์ง ์์๋ณด์. ์คํ๋ง๋ถํธ์์ ์ฌ์ฉํ ์ ์๋ ํ ํ๋ฆฟ ์์ง์๋ Thymeleaf, Mustache, Groovy, Freemarker, Velocity ๋ฑ์ด ์๋ค. ์ด ์ฑ ์์๋ ์คํ๋ง ์ง์์์ ์ถ์ฒํ๋ ํ์๋ฆฌํ(Thymleaf) ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
- ํ์๋ฆฌํ - https://www.thymeleaf.org/
ํ์๋ฆฌํ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ์ค์น๊ฐ ํ์ํ๋ค.









<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<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" />
<title>์ง๋ฌธ ๋ฆฌ์คํธ</title>
</head>
<body>
<h2>ํฌ๋ก์ฐ ํ์๋ฆฌํ</h2>
</body>
</html>


VScode์์ ์์ฑํจ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<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" />
<title>์ง๋ฌธ ๋ฆฌ์คํธ</title>
</head>
<body>
<h2>ํฌ๋ก์ฐ ํ์๋ฆฌํ</h2>
<table>
<thead>
<tr>
<th>์ ๋ชฉ</th>
<th>์์ฑ์ผ์</th>
</tr>
</thead>
<tbody>
<tr th:each="question : ${qList}">
<td th:text="${question.subject}"></td>
<td th:text="${question.createDate}"></td>
</tr>
</tbody>
</table>
</body>
</html>
QuestionController
package com.mysite.sbb.question;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class QuestionController {
@Autowired
private QuestionRepository qRepo;
@GetMapping("/question/list")
public String list(Model model) {
List<Question> qList = qRepo.findAll();
model.addAttribute("qList", qList);
return "question_list";
}
}


๊ธฐ๋ณธํ์ด์ง๋ ์ง๋ฌธ๋ชฉ๋ก ํ์ด์ง๋ก ๊ฒฝ๋ก ์ค์
package com.mysite.sbb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@GetMapping("/")
@ResponseBody
public String index() {
return "redirect:/question/list";
}
}



'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ต๋ณ ๋ฑ๋กํ๊ธฐ (0) | 2023.11.09 |
|---|---|
| Question ์๋น์ค ์์ฑ ๋ฐ ์์ธ๋ณด๊ธฐ ํ์ด์ง (0) | 2023.11.08 |
| ํจํค์ง ๋๋ฉ์ธ ๋ณ ๋ถ๋ฆฌํ๊ธฐ (0) | 2023.11.08 |
| ๋ต๋ณ AnswerRepository ์์ฑ ๋ฐ ๋ฐ์ดํฐ ์ ์ฅ ,๊ฒ์ (0) | 2023.11.08 |
| Repository , JUnit ํ ์คํธ (1) | 2023.11.08 |