2016년 9월 22일 목요일

43day / WEB / Model2 Architecture회원관리 Project

http://www.codeproject.com
Model 2 Architecture 회원관리 Project

  • Review
이번 Project 핵심 부분이였던
Dispatcher ServletHandlerMapping
ex)
Factory pattern 적용 객체 컨트롤러 구현체의 생성을 전담하는 객체
DispatcherServlet과 Controller 구현체의 결합도를 낮추는 효과를 준다
HandlerMapping(담당컨트롤러 연결 객체)은 시스템 상에 단 한번 생성
Singleton Pattern 적용하여 구현한다.
public class HandlerMapping {
    private static HandlerMapping instance = new HandlerMapping();
    private HandlerMapping() {
    }
    public static HandlerMapping getInstance() {
        return instance;
    }
* client로부터 전달된 command 에 따라 컨트롤러 구현객체를 생성하는 메서드
    public Controller create(String command) {
        Controller c = null;
        if (command.equals("findbyid")) {
            c = new FindByIdController();
        } else if (command.equals("findbyaddress")) {
            c = new FindByAddressController();
        } else if (command.equals("login")) {
            c = new LoginController();
        } else if (command.equals("logout")) {
            c = new LogoutController();
        } else if (command.equals("update")) {
            c = new UpdateController();
        } else if (command.equals("idcheck")) {
            c = new IdCheckController();
        } else if (command.equals("register")) {
            c = new RegisterController();
        }
        return c;
    }
}

1. request 즉 요청 분석 command를 반환받는다 
2. HandlerMapping(Factory) 를 이용해 
    Controller Interface 타입의 구현객체를 반환받는다 
3. Controller Interface의 표준화된 추상메서드를 
    호출하여 구현체(컨트롤러) 를 실행시킨다 
4. 실행 후 반환된 View url 정보를 이용해 forward or redirect로 응답한다 
    발생되는 Exception 을 처리해 콘솔에 메세지를 출력하고
    error.jsp로 redirect 시킨다. 
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public DispatcherServlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        handleRequest(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        handleRequest(request, response);
    }
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String command = request.getParameter("command");
        try {
            Controller c = HandlerMapping.getInstance().create(command);
            String url = c.execute(request, response);
            if (url.startsWith("redirect:"))
                response.sendRedirect(url.substring(9));
            else
                request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}
cs

DispatcherServlet적용하면서 parameter를 주고받는 방식이나
redirect방식에 어려움을 겪기도 하였고 어떤 부분을 Model과 Contoller로
나눠야 할지 어떻게 분담해야할지 그냥 Servlet이나 JSP에서
한번에 처리해버리면 안될까? 생각도 하였다.

사실 Model1이니 Model2이니 직접 써보지 않고서 느껴보기는 힘들었다.
강사님이 주신 공통 Code를 보아도 이해도 힘들었지만 힘들게 따라가다
이해하고 보니 우와 이런 구조였구나하며 감탄을 했다.
코드를 보고 놀라거나 감동받아 본 적은 있지만 코드를 보며
구조적으로 감탄해본 적은 처음이다.
강사님은 결국 이것이 spring이다!라며 말씀하셨지만
MVC2 구조 참 마음에 든다!

0 개의 댓글:

댓글 쓰기