Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Proxy
- db
- 스프링 핵심 기능
- java
- 백준
- Servlet
- Thymeleaf
- 그리디
- 알고리즘
- 스프링 핵심 원리
- QueryDSL
- springdatajpa
- spring
- 김영한
- SpringBoot
- Exception
- http
- AOP
- kotlin
- JDBC
- 인프런
- pointcut
- Android
- 스프링
- transaction
- Spring Boot
- JPQL
- jpa
- 자바
- Greedy
Archives
- Today
- Total
개발자되기 프로젝트
FrontController 도입 : v1 본문
1. 프론트 컨트롤러 도입 v1
2. Controller v1
- 다형성 활용 위해 interface를 도입.
- 각Controller는 Controller interface를 구현함.
- Controller Interface
public interface ControllerV1 {
void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
- MemberFormControllerV1 class
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); //이동!
}
}
- MemberListControllerV1 class
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.jps";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
- MemberSaveControllerV1 class
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);
//model에 data를 보관해보자.(K&V)
request.setAttribute("member", member);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
- FrontController
- Servlet
- Map을 활용하여 URI와 Controller를 매핑해둠.
@WebServlet(name = "FrontControllerServletV1", urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {
private Map<String, ControllerV1> controllerMap = new HashMap<>();
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 {
System.out.println("FrontControllerServletV1.service");
String requestURI = request.getRequestURI();
ControllerV1 controller = controllerMap.get(requestURI);
if(controller == null){
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
controller.process(request, response);
}
}
3. GitHub : 210910 FrontController
'인프런 > [인프런] 스프링 MVC 1' 카테고리의 다른 글
Model 추가 : v3 (0) | 2021.09.11 |
---|---|
Controller->view이동 분리 : V2 (0) | 2021.09.11 |
Front Controller 패턴 (0) | 2021.09.10 |
[Servlet, JSP] MVC 패턴 - 한계, 정리 (0) | 2021.09.09 |
[Servlet, JSP] MVC 적용 (0) | 2021.09.09 |
Comments