Q) 기존 static홈화면이 안나오고 HomeController가 응답하는 이유?
@Controller
public class HomeController {
@GetMapping("/") //처음 화면
public String home(){
return "home";
}
}
@Controller
public class MemberController {
/*
private final MemberService memberService = new MemberService();
기능이 별로 없어 new로 일일히 생성할 필요 없음
*/
/*
스프링 컨태이너에 등록하고 쓰면된다
컨테이너에서 memberService 연결
@Autowired면 스프링 컨테이너에서 memberService를 가져온다
*/
private final 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:/";
}
}
<input type="text" id="name" name="name" placeholder="이름을 입력하세요"/>
@GetMapping("/members")
public String list(Model model){
List<Member> members = memberService.findMembers();
model.addAttribute("members",members);
return "members/memberList";
}
<tbody>
<tr th:each="member : ${members}">
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>