ํ์ฅ ํ๋ก๊ทธ๋จ ์ค์ code Spell Checker ์ ์๋จ์ด๊ฐ ํ๋ฆด ๊ฒฝ์ฐ ์๋ ค์ฃผ๋ ํ๋ก๊ทธ๋จ์


๋ต๋ณ ๋ฑ๋ก ํผ์ ๋ง๋ ๋ค.
<h1 th:text="${question.subject}"></h1>
<div th:text="${question.content}"></div>
<!-- 1) __${}__ 2)'/answer/create/'+${question.id} 3)|/answer/create/${question.id}|-->
<form th:action="@{|/answer/create/${question.id}|}" method="post">
<textarea name="content" id="content" rows="10"></textarea>
<input type="submit" value="๋ต๋ณ๋ฑ๋ก" />
</form>
action ์ @์์ ์ฃผ์์์ id๊ฐ(๋ณ์)์ ๋ฃ๊ธฐ ์ํด์ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋๊ฐ ์๋ค.



http://localhost:8080/answer/create/2
AnswerController ์์ฑ

package com.mysite.sbb.answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.mysite.sbb.question.Question;
import com.mysite.sbb.question.QuestionService;
@Controller
@RequestMapping("/answer")
public class AnswerController {
@Autowired
private QuestionService qService;
@PostMapping("/create/{id}") //์ฃผ์์ ์๋ ๋ณ์ { }์์ ๋ฃ๊ธฐ
public String createAnswer(Model model, @PathVariable("id") Integer id,
@RequestParam String content) {
Question question = this.qService.getQuestion(id);
//TODO:๋ต๋ณ์ ์ ์ฅํ๋ค
return String.format("redirect:/question/detail/%s", id);
}
}
AnswerService ์์ฑ
package com.mysite.sbb.answer;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mysite.sbb.question.Question;
@Service
public class AnswerService {
@Autowired
private AnswerRepository aRepo;
//๋ต๋ณ ์ ์ฅ ์ ์ง๋ฌธ ๊ฐ์ฒด์ ๋ต๋ณ ๋ด์ฉ์ ์
๋ ฅ๋ฐ์์ ๋ต๋ณ์ ๋ฑ๋ก
public void create(Question question,String content ) {
Answer answer = new Answer(); //๋ต๋ณ ๊ฐ์ฒด ๋ง๋๋ฆญ
answer.setContent(content);
answer.setCreateDate(LocalDateTime.now());//์ค๋๋ ์ง
answer.setQuestion(question);
this.aRepo.save(answer);//์ ๋ต๋ณ ์
๋ ฅ
}
}
AnswerController ์์
//๋ต๋ณ ์ ์ฅ
@PostMapping("/create/{id}") //์ฃผ์์ ์๋ ๋ณ์ { }์์ ๋ฃ๊ธฐ
public String createAnswer(Model model, @PathVariable("id") Integer id,
@RequestParam String content) {
Question question = this.qService.getQuestion(id);
//๋ต๋ณ ์ ์ฅ
aService.create(question, content);
return String.format("redirect:/question/detail/%s", id);//์ง๋ฌธ ์์ธ๋ณด๊ธฐ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ
}
DB์ ์ ์ ์ฅ๋๋์ง ํ ์คํธํด๋ณด๊ธฐ


๊ทธ๋ฆฌ๊ณ
question_detail ์ ๋ฑ๋ก๋ ๋ต๋ณ ํ์ธ ํ ์ ์๊ฒ ์์
</head>
<body>
<h1 th:text="${question.subject}"></h1>
<div th:text="${question.content}"></div>
<!-- ๋ต๋ณ ์ถ๋ ฅ -->
<h5
th:text="${#lists.size(question.answerList)+'๊ฐ์ ๋ต๋ณ์ด ์์ต๋๋ค.'}"
></h5>
<div>
<ul>
<li
th:each="answer : ${question.answerList}"
th:text="${answer.content}"
></li>
</ul>
</div>
<!-- 1) __${}__ 2)'/answer/create/'+${question.id} 3)|/answer/create/${question.id}|-->
<form th:action="@{|/answer/create/${question.id}|}" method="post">
<textarea name="content" id="content" rows="10"></textarea>
<input type="submit" value="๋ต๋ณ๋ฑ๋ก" />
</form>
</body>
#lists.size(question.answerList)}๋ ๋ต๋ณ ๊ฐ์๋ฅผ ์๋ฏธํ๋ค.
#lists.size(์ดํฐ๋ฌ๋ธ๊ฐ์ฒด)๋ ํ์๋ฆฌํ๊ฐ ์ ๊ณตํ๋ ์ ํธ๋ฆฌํฐ๋ก ๊ฐ์ฒด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ค.
๋ต๋ณ์ question ๊ฐ์ฒด์ answerList๋ฅผ ์ํํ์ฌ <li> ์๋ฆฌ๋จผํธ๋ก ํ์ํ๋ค.
ํ ์คํธํ๊ธฐ


'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ง๋ฌธ ๋ชฉ๋ก/์์ธ๋ณด๊ธฐ - ๋ถํธ์คํธ๋ฉ ์ ์ฉ (0) | 2023.11.09 |
|---|---|
| ์คํํฑ ๋๋ ํฐ๋ฆฌ์ ์คํ์ผ์ํธ (0) | 2023.11.09 |
| Question ์๋น์ค ์์ฑ ๋ฐ ์์ธ๋ณด๊ธฐ ํ์ด์ง (0) | 2023.11.08 |
| Question ์ปจํธ๋กค๋ฌ ์์ฑ (1) | 2023.11.08 |
| ํจํค์ง ๋๋ฉ์ธ ๋ณ ๋ถ๋ฆฌํ๊ธฐ (0) | 2023.11.08 |