View 분리 - v2

shinyeongwoon·2022년 12월 26일
0

spring MVC

목록 보기
14/32
post-custom-banner

view 분리 - v2

모든 컨트롤러에서 뷰로 이동하는 부분에 중복이 있고, 깔끔하지 않다.

String viewPath = "/WEB-INF/views/new-form.jsp"
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request,response);

이 부분을 깔끔하게 분리하기 위해 별도로 뷰를 처리하는 객체를 만들어 보자

V2 구조

MyView

뷰 객체는 이후 다른 버전도 사용하므로 패키지 위치를 frontcontroller에 두었다.
경로 : hello.servlet.web.frontcontroller

  package hello.servlet1.web.frontcontroller;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyView {

    private String viewPath;

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

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

}

ControllerV2 interface 만들기

경로 : hello.servlet.web.frontcontroller.v2

package hello.servlet1.web.frontcontroller.v2;

import hello.servlet1.web.frontcontroller.MyView;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;
import java.io.IOException;

public interface ControllerV2 {

    MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

}

회원 등록 폼

경로 : hello.servlet.web.frontcontroller.v2.controller

package hello.servlet1.web.frontcontroller.v2.controller;

import hello.servlet1.web.frontcontroller.MyView;
import hello.servlet1.web.frontcontroller.v2.ControllerV2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MemberFormControllerV2 implements ControllerV2 {
    @Override
    public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return new MyView("/WEB-INF/views/new-form.jsp");
    }
}

이제 각 컨트롤러는 복잡한 dispatcher.forward()를 직접 생성해서 호출하지 않아도 된다.
단순히 MyView 객체를 생성하고 거기에 뷰 이름만 넣고 반환하면 된다.

ControllerV1을 구현한 클래스와 ControllerV2를 구현한 클래스를 비교해보면 이 부분의 중복이 확실하게 제거된 것을 확인할 수 있다.

회원 저장

경로 : hello.servlet.web.frontcontroller.v2.controller

package hello.servlet1.web.frontcontroller.v2.controller;

import hello.servlet1.domain.member.Member;
import hello.servlet1.domain.member.MemberRepository;
import hello.servlet1.web.frontcontroller.MyView;
import hello.servlet1.web.frontcontroller.v2.ControllerV2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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);

        request.setAttribute("member",member);

        return new MyView("/WEB-INF/views/save-result.jsp");

    }
}

회원 목록

경로 : hello.servlet.web.frontcontroller.v2.controller

package hello.servlet1.web.frontcontroller.v2.controller;

import hello.servlet1.domain.member.Member;
import hello.servlet1.domain.member.MemberRepository;
import hello.servlet1.web.frontcontroller.MyView;
import hello.servlet1.web.frontcontroller.v2.ControllerV2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

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.jsp");

    }
}

프론트 컨트롤러

경로 : hello.servlet.web.frontcontroller.v2

package hello.servlet1.web.frontcontroller.v2;

import hello.servlet1.web.frontcontroller.MyView;
import hello.servlet1.web.frontcontroller.v2.controller.MemberFormControllerV2;
import hello.servlet1.web.frontcontroller.v2.controller.MemberListControllerV2;
import hello.servlet1.web.frontcontroller.v2.controller.MemberSaveControllerV2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@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);


    }
}

ControllerV2의 반환 타입이 MyView이므로 프론트 컨트롤러는 컨트롤러 호출 결과로 MyView 를 반환 받는다.
그리고 view.render()를 호출하면 forward로직을 수행해서 JSP가 실행 된다.

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

프론트 컨트롤러의 도입으로 MyView 객체의 render()를 호출하는 부분을 모두 일관되게 처리할 수 있다. 각각 컨트롤러는 MyView 객체를 생성만 해서 반환하면 된다.

실행 )
등록 : http://localhost:8080/front-controller/v2/members/new-form
목록 : http://localhost:8080/front-controller/v2/members

post-custom-banner

0개의 댓글