스프링 입문 - 7 웹 MVC 개발 (등록, 조회)

CodeKong의 기술 블로그·2023년 7월 6일
1

SPRING BOOT

목록 보기
7/24
post-thumbnail

Controller/HomeController

@Controller
public class HomeController {
   @GetMapping("/")
   public String home() {
   return "home";
   }
}

Controller/MemberController

@Controller
public class MemberController {
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:/";
}

@GetMapping("/members")
public String list(Model model) {
    List<Member> members = memberservice.findMembers();
    model.addAttribute("members", members);

    return "members/memberList";
}

}

0개의 댓글