구조
코드
//HomeController.java
package hello.helloSpring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(){
return "home";
}
}
//MemberController.java
package hello.helloSpring.controller;
import hello.helloSpring.domain.Member;
import hello.helloSpring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class MemberController {
private final MemberService memberService;
@Autowired // 스프링이 뜰 때, 컨테이너에서 관리하는 memberService 객체와 연결해준다.
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
@GetMapping("/members/new")
public String createMemberForm(){
return "/members/createMemberForm";
}
@PostMapping("/members/new")
public String create(MemberForm memberForm){
Member member = new Member();
member.setName(memberForm.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";
}
}
//MemberForm
package hello.helloSpring.controller;
public class MemberForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
html 파일들은 생략..
결과
고작 이거 만드는 게 이렇게 힘들다니~
'Backend > Spring' 카테고리의 다른 글
[인프런 스프링 입문] AOP (0) | 2021.01.10 |
---|---|
[인프런 스프링 입문] 스프링 DB 접근 기술 (0) | 2021.01.09 |
[인프런 스프링 입문] 스프링 빈과 의존관계 (0) | 2021.01.07 |
[인프런 스프링 입문] 회원 관리 예제 - 백엔드 개발 (0) | 2021.01.06 |
[인프런 스프링 입문] 스프링 웹 개발 기초 (0) | 2021.01.05 |