본 포스트는 스프링 MVC 1편(김영한, 인프런) 강의를 통해 학습한 내용을 작성자 임의 대로 요약 및 정리한 것입니다.
1. View 분리 - v2
1.1. 이전 문제점
1
2
3
4
5
String viewPath
= "/WEB-INF/views/new-form.jsp";
RequestDispatcher dispatcher
= request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
모든 컨트롤러에서
뷰로 이동하는 부분에 중복이 있고 깔끔하지 않음!
==> 별도로 뷰를 처리하는 객체 만들기!!!
1.2. V1의 구조
- 클라이언트의 HTTP 요청
- FrontController –> 매핑 정보
: URL 매핑 정보에서 컨트롤러 조회 - FrontController –> Controller
: 컨트롤러 호출 - Controller –> FrontController
:MyView
반환 - FrontController –> MyView
:render()
호출 - MyView –> JSP
: JSP forward - JSP는 HTML 응답.
1.3. MyView
/servlet/web/frontcontroller
에서
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}
}
MyView
의 객체는 다른 버전에서도 함께 사용 예정.
1.4. ControllerV2
/servlet/web/frontcontroller/v2
에서
1
2
3
4
5
6
public interface ControllerV2 {
MyView process(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
}
1.4.1 MemberFormControllerV2
- 회원 등록 폼
/servlet/web/frontcontroller/v2/controller
에서
1
2
3
4
5
6
7
8
9
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
객체 생성 후 뷰 이름만 넣고 반환
1.4.2. MemberSaveControllerV2
- 회원 저장
/servlet/web/frontcontroller/v2/controller
에서
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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에 데이터를 보관한다.
request.setAttribute("member", member);
return new MyView("/WEB-INF/views/save-result.jsp");
}
}
1.4.3 MemberListControllerV2
- 회원 목록
/servlet/web/frontcontroller/v2/controller
에서
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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");
}
}
1.4.4 FrontControllerServletV2
- 프론트 컨트롤러
/servlet/web/frontcontroller/v2
에서
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
32
33
34
35
36
37
38
39
40
41
42
43
@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);
}
}