๐ŸŒฑ Spring MVC (2) ๋‹คํ˜•์„ฑ์„ ์ด์šฉํ•œ FrontController (Dispatcher Servlet) ๊ตฌํ˜„

Kim Dae Hyunยท2021๋…„ 7์›” 1์ผ
0

Spring-MVC

๋ชฉ๋ก ๋ณด๊ธฐ
3/13
post-thumbnail

Github ์†Œ์Šค์ฝ”๋“œ

๐Ÿ”Ž ์ด๋Ÿฐ๊ฑฐ ํ•ด๋ณผ ๊ฑฐ์—์š”

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„ ์ ํ•ฉํ•œ Controller๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š” FrontController๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐœ Controller๋ฅผ ๋งคํ•‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” FrontController๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ด๊ณ  ๊ฐ Controller๋‚˜ ํ”„๋กœ์ ํŠธ ์„ค์ •์€ ์ด์ „ ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.


1. ๋‹คํ˜•์„ฑ์„ ์œ„ํ•œ Controller ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

  • ๊ฐ Controller๋Š” request์™€ response๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š” ํ•œ ๊ฐœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–๋Š”๋‹ค.
public interface ControllerV1 {

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

2. ๋‹คํ˜•์„ฑ์„ ์œ„ํ•œ Controller ๊ตฌํ˜„์ฒด ์ž‘์„ฑ

๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•œ Form์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ Controller

public class MemberFormControllerV1 implements ControllerV1 {

    @Override
    public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String viewPath = "/WEB-INF/views/new-form.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
        dispatcher.forward(request, response);
    }
}

์ „์†ก๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ Controller

public class MemberSaveControllerV1 implements ControllerV1 {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    public void 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);

        String viewPath = "/WEB-INF/views/save-result.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
        request.setAttribute("member",member);
        dispatcher.forward(request, response);
    }
}

์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ Controller

public class MemberListControllerV1 implements ControllerV1 {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Member> members = memberRepository.findAll();

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

3. FrontController ์ž‘์„ฑ

  • Map์„ ์ด์šฉํ•˜์—ฌ URI์™€ Controller์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • Key๋กœ๋Š” URI์ •๋ณด๋ฅผ, Value๋กœ๋Š” Controller์˜ ๊ตฌํ˜„์ฒด๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งคํ•‘์ •๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์„ธํŒ…ํ•ด๋‘ก๋‹ˆ๋‹ค.
  • request.getRequestURI() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์š”์ฒญ๋œ URI๊ฐ’์„ ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.
    • URI๋Š” localhost:8080/test/members๊ฐ€ ์žˆ๋‹ค๋ฉด /test/member ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
  • ํŒŒ์‹ฑ๋œ URI๋ฅผ KEY๊ฐ’์œผ๋กœ ํ•˜์—ฌ ๋งคํ•‘์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ €์žฅ์†Œ์—์„œ ์ ํ•ฉํ•œ ๊ตฌํ˜„์ฒด๋ฅผ ์–ป์–ด์˜จ ํ›„ ๊ตฌํ˜„์ฒด๋งˆ๋‹ค ๊ตฌํ˜„ํ•œ process()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
@WebServlet(name = "frontControllerServletV1", urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {

    // URI ๋งคํ•‘์ •๋ณด์™€ Controller์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ €์žฅ
    private Map<String, ControllerV1> controllerMap = new HashMap<>();

    // URI์— ๋”ฐ๋ผ Controller์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑ
    public FrontControllerServletV1() {
        controllerMap.put("/front-controller/v1/members/new-form", new MemberFormControllerV1());
        controllerMap.put("/front-controller/v1/members/save", new MemberSaveControllerV1());
        controllerMap.put("/front-controller/v1/members", new MemberListControllerV1());
    }

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

        // URI ๋งคํ•‘์ •๋ณด ํŒŒ์‹ฑ
        String requestURI = request.getRequestURI();
        // ํŒŒ์‹ฑ๋œ URI์™€ ์ ํ•ฉํ•œ ๊ตฌํ˜„์ฒด๋ฅผ get
        ControllerV1 controller = controllerMap.get(requestURI);
        if (controller == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // ์ ํ•ฉํ•œ ๊ตฌํ˜„์ฒด์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        controller.process(request, response);
    }
}

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง MVC 1ํŽธ ์„ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€