728x90
//ํ์๋ฑ๋ก ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
controller > HomeController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(){
return "home";
}
}
localhost:8080/home
Spring ์ ์คํ์ํค๋ฉด GetMapping ์ ๋จผ์ ์ฐพ์์ ํ๋ฉด์ ๋ณด์ฌ์ค๋ค. GetMapping ์ด ์์ผ๋ฉด ๊ทธ ๋ static ํ์ผ์ ์ฐพ์์ ๋ณด์ฌ์ค๋ค.
templates > home.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class ="container">
<div>
<h1>Hello Spring</h1>
<p>ํ์ ๊ธฐ๋ฅ</p>
<p>
<a href="/members/new">ํ์ ๊ฐ์
</a>
<a href="/members">ํ์ ๋ชฉ๋ก</a>
</p>
</div>
</div>
</body>
</html>
์คํํ๋ฉด
controller > MemberController.java
import com.yoonsung.firstproject.domain.Member;
import com.yoonsung.firstproject.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class MemberController {
private MemberService memberService;
@Autowired
public MemberController(MemberService memberService){
this.memberService = memberService;
}
@GetMapping("members/new")
public String createForm(){
return "members/createMemberForm";
}
@PostMapping("members/new")
public String create(MemberForm form){
Member member = new Member();
member.setName(form.getName());
memberService.join(member);
return "redirect:/";
}
}
localhost:8080/members/new
templates > members > createMemberForm.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class = "container">
<form action="/members/new" method="post">
<div class="form-group">
<label for="name">์ด๋ฆ</label>
<input type="text" id="name" name="name" placeholder="์ด๋ฆ์ ์
๋ ฅํ์ธ์">
</div>
<button type="submit">๋ฑ๋ก</button>
</form>
</div>
</body>
</html>
/members/new ์ post ๋ฐฉ์์ผ๋ก ๋๊ฒจ์ง๋ค.
(*post๋ฐฉ์: form ํํ๋ก ๊ฐ์ ๋๊ธธ ๋ ์ฌ์ฉํ๋ ๋ฐฉ์)
name ์ด ๊ฐ์ ๋๊ฒจ์ฃผ๋ key ๊ฐ ๋๋ค.
์คํํ๋ฉด
//ํ์ ์กฐํ ๊ธฐ๋ฅ
templates > members > memberList.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<div>
<table>
<thead>
<tr>
<th>#</th>
<th>์ด๋ฆ</th>
</tr>
</thead>
<tbody>
<tr th:each="member : ${members}">
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
foreach ์ฒ๋ผ members ๋ฅผ ๋๋ฉฐ member ์ ๋ณด๋ฅผ ํ๋์ฉ ์ถ๋ ฅํ๋ค.
์คํํ๋ฉด
728x90
'๐ฅ๏ธ Web Development Study > Back-End' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Back-end] ์คํ๋ง ์ ๋ฌธ :: AOP (1) | 2025.01.29 |
---|---|
[Back-end] ์คํ๋ง ์ ๋ฌธ :: ์คํ๋ง DB ์ ๊ทผ ๊ธฐ์ (0) | 2025.01.29 |
[Back-End] ์คํ๋ง ์ ๋ฌธ :: ์คํผ๋ง ๋น๊ณผ ์์กด๊ด๊ณ (1) | 2025.01.23 |
[Back-End] ์คํ๋ง ์ ๋ฌธ :: ํ์ ๊ด๋ฆฌ ์์ - ๋ฐฑ์๋ ๊ฐ๋ฐ (0) | 2024.12.25 |
[Back-End] ์คํ๋ง ์ ๋ฌธ :: Spring ์น ๊ฐ๋ฐ ๊ธฐ์ด (0) | 2024.12.22 |