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 V5 - 2 본문

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

Controller V5 - 2

Seung__ 2021. 9. 12. 00:02

V5는 V3, V4 Controller 모두 대응이 가능하도록 작성해보자.

 

FrontControllerV5에 ControllerV4도 추가해보자.

 

1. ControllerV4HandlerAdapter


  • ControllerV4는 process() 실행 후 String viewName을 return한다.
  • MyHandlerAdapter 인터페이스는 기본적으로 handler()에서 controller의 process()를 호출하고 ModelView를 반환.
  • 따라서 Adapter에서 ModelView를 반환해야 하기 때문에 ControllerV4를 위한 Adapter는
  • 별도로 ModelView생성필요.
public class ControllerV4HandlerAdapter implements MyHandlerAdapter {
    @Override
    public boolean supports(Object handler) {
        return (handler instanceof ControllerV4);
    }

    @Override
    public ModelView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        ControllerV4 controller = (ControllerV4) handler;

        HashMap<String, Object> model = new HashMap<>();
        Map<String, String> paramMap = createParamMap(request);
        String viewName = controller.process(paramMap, model);

        ModelView mv = new ModelView(viewName);
        mv.setModel(model);
        return mv;
    }

    private Map<String, String> createParamMap(HttpServletRequest request) {
        //paramMap
        Map<String, String> paramMap = new HashMap<>();
        request.getParameterNames().asIterator()
                .forEachRemaining(paramName -> paramMap.put(paramName, request.getParameter(paramName)));
        return paramMap;
    }
}

 

 

 

2. FrontControllerV5


  • HandlerMapping, HandlerAdapters만 변경되면 됨.
    private void initHandlerMappingMap() {
        handlerMappingMap.put("/front-controller/v5/v3/members/new-form", new MemberFormControllerV3());
        handlerMappingMap.put("/front-controller/v5/v3/members/save", new MemberSaveControllerV3());
        handlerMappingMap.put("/front-controller/v5/v3/members", new MemberListControllerV3());

        handlerMappingMap.put("/front-controller/v5/v4/members/new-form", new MemberFormControllerV4());
        handlerMappingMap.put("/front-controller/v5/v4/members/save", new MemberSaveControllerV4());
        handlerMappingMap.put("/front-controller/v5/v4/members", new MemberListControllerV4());
    }

    private void initHandlerAdapters() {
        handlerAdapters.add(new ControllerV3HandlerAdapter());
        handlerAdapters.add(new ControllerV4HandlerAdapter());
    }

 

 

3. GitHub : 210911 FrontController V5-2


 

GitHub - bsh6463/MVC1

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

github.com

 

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

Handler 매핑, Handler 어댑터  (0) 2021.09.12
Spring MVC 전체 구조  (0) 2021.09.12
Controller V5 -1  (0) 2021.09.11
Controller V4  (0) 2021.09.11
Model 추가 : v3  (0) 2021.09.11
Comments