Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

SpringMVC - 콘트롤러 통함 본문

인프런/[인프런] 스프링 MVC 1

SpringMVC - 콘트롤러 통함

Seung__ 2021. 9. 12. 23:10

1. Controller 통합


  • @RequestMapping는 메서드 단위에 적용된 것을 확인할 수 있다.
  • 따라서 컨트롤러 클래스를 유연하게 하나로 통합할 수 있다.
  • 클래스에 붙은 @RequestMapping()에 url의 공통 부분을 넣어도 가능.210912 SpringMVC
@Controller
@RequestMapping("/springmvc/v2/members")
public class SpringMemberControllerV2 {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @RequestMapping("/new-form")
    public ModelAndView newForm(){
        return new ModelAndView("new-form");
    }

    @RequestMapping("/save")
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response){

        String username = request.getParameter("username");
        int age = Integer.parseInt(request.getParameter("age"));

        Member member = new Member(username, age);

        memberRepository.save(member);

        ModelAndView mv = new ModelAndView("save-result");
        //mv.getModel().put("member", member);
        mv.addObject("member", member);
        return mv;
    }

    @RequestMapping
    public ModelAndView members() {
        List<Member> members = memberRepository.findAll();

        ModelAndView mv = new ModelAndView("members");

        mv.addObject("members", members);

        return mv;

    }
}

 

 

2. GitHub : 210912 SpringMVC


 

GitHub - bsh6463/MVC1

Contribute to bsh6463/MVC1 development by creating an account on GitHub.

github.com

 

'인프런 > [인프런] 스프링 MVC 1' 카테고리의 다른 글

프로젝트 생성  (0) 2021.09.13
SpringMVC - 실용적인방식.  (0) 2021.09.12
Spring MVC  (0) 2021.09.12
ViewResolver  (0) 2021.09.12
Handler 매핑, Handler 어댑터  (0) 2021.09.12
Comments