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
관리 메뉴

개발자되기 프로젝트

Controller->view이동 분리 : V2 본문

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

Controller->view이동 분리 : V2

Seung__ 2021. 9. 11. 00:15

1. Controller->view이동 분리


  • 모든 Controller에 View로 이동하는 코드가 있다.. 귀찮.
String viewPath = "/WEB-INF/views/new-form.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response); //이동!

 

 

2. v2 구조


 

 

 

3. MyView


  • Controller ->View이동하는 부분 담당.
public class MyView {

    private String viewPath;

    public MyView(String viewPath) {
        this.viewPath = viewPath;
    }

    public void render(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        RequestDispatcher dispatch = request.getRequestDispatcher(viewPath);
        dispatch.forward(request,response);
    }
}

 

 

4. Controller V2


  • Interface
  • 각 Controller는 Myview를 return함
  • FrontController에서 Myview의  render()를 실행하여 JSP로 이동함.
public interface ControllerV2 {

    MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
public class MemberFormControllerV2 implements ControllerV2 {

    @Override
    public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MyView myView = new MyView("/WEB-INF/views/new-form.jsp");
        return myView;
    }

}
public class MemberListControllerV2 implements ControllerV2 {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Member> members = memberRepository.findAll();

        //모델에 담기
        request.setAttribute("members", members);

        return new MyView("/WEB-INF/views/members.jps");
    }
}
public class MemberSaveControllerV2 implements ControllerV2 {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

        Member member = new Member(username, age);
        memberRepository.save(member);

        //model에 data를 보관해보자.(K&V)
        request.setAttribute("member", member);

        return new MyView("/WEB-INF/views/save-result.jsp");
    }
}
@WebServlet(name = "FrontControllerServletV2", urlPatterns = "/front-controller/v2/*")
public class FrontControllerServletV2 extends HttpServlet {

    private Map<String, ControllerV2> controllerMap = new HashMap<>();

    public FrontControllerServletV2() {

        controllerMap.put("/front-controller/v2/members/new-form", new MemberFormControllerV2());
        controllerMap.put("/front-controller/v2/members/save", new MemberSaveControllerV2());
        controllerMap.put("/front-controller/v2/members", new MemberListControllerV2());
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String requestURI = request.getRequestURI();

        ControllerV2 controller = controllerMap.get(requestURI);
        if(controller == null){
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        MyView view = controller.process(request, response);
        view.render(request,response);


    }
}

 

 

 

5. 정리 


프론트 컨트롤러의 도입으로 MyView 객체의 render() 를 호출하는 부분을 모두 일관되게 처리함.

각 컨트롤러는 MyView 객체를 생성만 해서 반환하면 된다.

 

 

6. GitHub : 210910 FrontController V2


 

GitHub - bsh6463/MVC1

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

github.com

 

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

Controller V4  (0) 2021.09.11
Model 추가 : v3  (0) 2021.09.11
FrontController 도입 : v1  (0) 2021.09.10
Front Controller 패턴  (0) 2021.09.10
[Servlet, JSP] MVC 패턴 - 한계, 정리  (0) 2021.09.09
Comments