[Spring] 4.MVC ํ๋ ์์ํฌ ๋ง๋ค๊ธฐ
1. ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ํจํด ์๊ฐ
[ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์ ์ ]
- ํด๋ผ์ด์ธํธ์ ํธ์ถ์ ๋ฐ๋ ์ ๊ตฌ๊ฐ ์์
[ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์ ํ]
- ์๋ฌธ์ฅ ์ญํ ํ๋ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ ํ๋๋ก ํด๋ผ์ด์ธํธ ์์ฒญ ๋ฐ์
- ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ์์ฒญ์ ๋ง๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐพ์์ ํธ์ถ
- ๊ณตํต ์ฒ๋ฆฌ ๊ธฐ๋ฅ
- ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ธํ ๋๋จธ์ง ์ปจํธ๋กค๋ฌ๋ ์๋ธ๋ฆฟ์ ์ฌ์ฉํ์ง ์์๋ ๋จ
์คํ๋ง ์น MVC์ ํต์ฌ๋ ๋ฐ๋ก 'Front Controller' 'DispatcherServlet'์ด Front Controller ํจํด์ผ๋ก ๊ตฌํ๋์ด์์ |
2. ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์ - v1
๋จ๊ณ์ ์ผ๋ก ๋์
๊ธฐ์กด ์ฝ๋๋ฅผ ์ต๋ํ ์ ์งํ๋ฉด์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์
๋จผ์ ๊ตฌ์กฐ๋ฅผ ๋ง์ถ๊ณ ์ ์ง์ ์ผ๋ก ๋ฆฌํฉํ ๋ง
'v1 ๊ตฌ์กฐ'
ControllerV1
public interface ControllerV1 {
void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
- ์ปจํธ๋กค๋ฌ ์ธํฐํ์ด์ค ๋์
- ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ปจํธ๋กค๋ฌ ๋ง๋ค์ด์ผํจ
- ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ ์ด ์ธํฐํ์ด์ค๋ฅผ ํธ์ถํด์ ๊ตฌํ๊ณผ ๊ด๊ณ์์ด ๋ก์ง์ ์ผ๊ด์ฑ ๊ฐ์ ธ๊ฐ ์ ์์
์ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋๋๊ฐ?
- ๊ฐ ์ปจํธ๋กค๋ฌ ๋ง๋ค๊ฑด๋ฐ ์์ฒญ ์ ๋ณด ๋ค์ด์์๋ ๋งตํ ์ ๋ณด ์ฐพ์์ ํธ์ถํ ๋ ์ผ๊ด์ฑ์๊ฒ ๋คํ์ฑ ์ฌ์ฉํด์ Front Controller๋ ์ธํฐํ์ด์ค์ ์์กดํ๋ฉด์ ํธ๋ฆฌํ๊ฒ ํธ์ถ ๊ฐ๋ฅ
ํ์ ๋ฑ๋ก ์ปจํธ๋กค๋ฌ
MemberFormControllerV1
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);
}
}
ํ์ ์ ์ฅ ์ปจํธ๋กค๋ฌ
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);
request.setAttribute("member", member);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
ํ์ ๋ชฉ๋ก ์ปจํธ๋กค๋ฌ
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);
}
}
์ค์) **
์ ์ปจํธ๋กค๋ฌ๋ค์ ๋ชจ๋ web-frontcontroller-v1-controller ํด๋ ์์ ์กด์ฌ
ํ์ง๋ง ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์ ๊ฒฝ์ฐ web-frontcontroleller-v1์ ์์ฑ
ํ๋ก ํธ ์ปจํธ๋กค๋ฌ
@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);
}
}
- v1/* : v1ํ์์ ๋ญ๊ฐ ๋ค์ด์๋ ์ง๊ธ ํด๋น ์๋ธ๋ฆฟ์ด ๋ฌด์กฐ๊ฑด ํธ์ถ๋จ
* ์ค์ ๊ฐ๋ฐํ ๋ ์ ๋๊ฒฝ๋ก๋ฅผ ๋ง์ด ์
* ํจํด ๋น์ทํ ๊ฒฝ์ฐ ์๋๊ฒฝ๋ก๋ฅผ ์จ๋ ๋จ
3. ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์ - v2
๋ชจ๋ ์ปจํธ๋กค๋ฌ์์ ๋ทฐ๋ก ์ด๋ํ๋ ๋ถ๋ถ์ ์ค๋ณต์ด ์๋ ๋ฌธ์ ํด๊ฒฐ
-> ๋ทฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด ์์ฑ
1) ํด๋ผ์ด์ธํธ๊ฐ HTTP ์์ฒญ
2) ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ์ปจํธ๋กค๋ฌ ์ฐพ์์์ ํธ์ถ
3) ์ปจํธ๋กค๋ฌ๊ฐ ์ง์ ํฌ์๋ฉํ์ง ์๊ณ MyView ๊ฐ์ฒด ๋ง๋ค์ด์ ๋ฐํํ๋ฉด,
4) ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ๋์ JSP ํฌ์๋ฉ
MyView
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);
}
}
ControllerV2
public interface ControllerV2 {
MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
ํ์ ๋ฑ๋ก ํผ
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() ํธ์ถ ํ์ ์์
ํ์ ์ ์ฅ
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);
request.setAttribute("member", member);
return new MyView("/WEB-INF/views/save-result.jsp");
}
}
ํ์ ๋ชฉ๋ก
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");
}
}
ํ๋ก ํธ ์ปจํธ๋กค๋ฌ V2
@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);
}
}
- ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ๋์ ์ผ๋ก MyView ๊ฐ์ฒด์ render() ํธ์ถํ๋ ๋ถ๋ถ์ ๋ชจ๋ ์ผ๊ด๋๊ฒ ์ฒ๋ฆฌ ๊ฐ๋ฅ
- ๊ฐ๊ฐ์ ์ปจํธ๋กค๋ฌ๋ MyView ๊ฐ์ฒด๋ฅผ ์์ฑ๋งํด์ ๋ฐํํ๋ฉด๋จ
4. Model ์ถ๊ฐ - v3
[์๋ธ๋ฆฟ ์ข ์์ฑ ์ ๊ฑฐ]
- ์ปจํธ๋กค๋ฌ ์ ์ฅ์์ request, response ํ์์์
- ์์ฒญ ํ๋ผ๋ฏธํฐ ์ ๋ณด < request.getParameter("") > ๋ ์๋ฐ์ Map์ผ๋ก ๋์ ๋๊ธฐ๋ฉด ์ง๊ธ ๊ตฌ์กฐ์์ ์ปจํธ๋กค๋ฌ๊ฐ ์๋ธ๋ฆฟ ๊ธฐ์ ๋ชฐ๋ผ๋ ๋์ ๊ฐ๋ฅ
- request๊ฐ์ฒด๋ฅผ Model๋ก ์ฌ์ฉํ๋ ๋์ ๋ณ๋์ Model ๊ฐ์ฒด ๋ง๋ค์ด์ ๋ฐํ
์ฆ ์ปจํธ๋กค๋ฌ๊ฐ ์๋ธ๋ฆฟ ๊ธฐ์ ์ ํ ์ฌ์ฉํ์ง ์๋๋ก
-> ์ฝ๋ ๋จ์ / ํ ์คํธ ์ฝ๋ ์์ฑ ์ฌ์์ง!
[๋ทฐ ์ด๋ฆ ์ค๋ณต ์ ๊ฑฐ]
- ์ปจํธ๋กค๋ฌ์์ ์ง์ ํ๋ ๋ทฐ ์ด๋ฆ์ ์ค๋ณต์ด ์๋ ๊ฒ์ ํ์ธ ๊ฐ๋ฅ
- ์ปจํธ๋กค๋ฌ๋ '๋ทฐ์ ๋ ผ๋ฆฌ ์ด๋ฆ'์ ๋ฐํ, ์ค์ ๋ฌผ๋ฆฌ ์์น์ ์ด๋ฆ์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌํ๋๋ก ๋จ์ํ
- ํฅํ ๋ทฐ์ ํด๋ ์์น๊ฐ ํจ๊ป ์ด๋ํด์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ง ๊ณ ์น๋ฉด ๋จ!
-> ๋ณ๊ฒฝ์ ์ง์ ์ ํ๋๋ก ๋ ์ ์์
/WEB-INF/views/new-form.jsp new-form
/WEB-INF/views/save-result.jsp save-result /WEB-INF/views/members.jsp members |
V3 ๊ตฌ์กฐ
ModelView
- ์๋ธ๋ฆฟ ์ข ์์ฑ ์ ๊ฑฐํ๊ธฐ ์ํด Model ์ง์ ๋ง๋ค๊ณ ์ถ๊ฐ๋ก View ์ด๋ฆ๊น์ง ์ ๋ฌํ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผํจ
- ModelView ๊ฐ์ฒด๋ ๋ค๋ฅธ ๋ฒ์ ์์๋ ์ฌ์ฉํ๋ฏ๋ก ํจํค์ง๋ฅผ frontcontroller์ ๋
public class ModelView {
private String viewName; // ๋ทฐ ๋
ผ๋ฆฌ์ ์ด๋ฆ
private Map<String, Object> model = new HashMap<>(); // ๋ชจ๋ธ
public ModelView(String viewName) {
this.viewName = viewName;
}
public String getViewName() {
return viewName;
}
public void setViewName(String viewName) {
this.viewName = viewName;
}
public Map<String, Object> getModel() {
return model;
}
public void setModel(Map<String, Object> model) {
this.model = model;
}
}
- ๋ทฐ ์ด๋ฆ๊ณผ ๋ทฐ ๋ ๋๋ง์ ํ์ํ model ๊ฐ์ฒด ๊ฐ์ง๊ณ ์์
ControllerV3
public interface ControllerV3 {
ModelView process(Map<String, String> paramMap);
}
- ํ๋ ์์ํฌ์ ์ข ์์ ์
- ์๋ธ๋ฆฟ์ ๋์ด์ ์ข ์์ ์ด์ง ์์
์ค๋ช )
์ด ์ปจํธ๋กค๋ฌ๋ ์๋ธ๋ฆฟ ๊ธฐ์ ์ ์ ํ ์ฌ์ฉํ์ง ์๋๋ค.
๋ฐ๋ผ์ ๊ตฌํ์ด ๋งค์ฐ ๋จ์ํด์ง๊ณ , ํ ์คํธ ์ฝ๋ ์์ฑ์ ํ ์คํธ ํ๊ธฐ ์ฝ๋ค. HttpServletRequest๊ฐ ์ ๊ณตํ๋ ํ๋ผ๋ฏธํฐ๋ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ paramMap์ ๋ด์์ ํธ์ถํด์ฃผ๋ฉด ๋๋ค. ์๋ต ๊ฒฐ๊ณผ๋ก ๋ทฐ ์ด๋ฆ๊ณผ ๋ทฐ์ ์ ๋ฌํ Model ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ ModelView ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉด ๋๋ค |
ํ์ ๋ฑ๋ก ํผ
public class MemberFormControllerV3 implements ControllerV3 {
@Override
public ModelView process(Map<String, String> paramMap) {
return new ModelView("new-form");
}
}
- ModelView ์์ฑํ ๋ new-form์ด๋ผ๋ view์ ๋ ผ๋ฆฌ์ ์ธ ์ด๋ฆ ์ง์
- ์ค์ ๋ฌผ๋ฆฌ์ ์ด๋ฆ์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌ
ํ์ ์ ์ฅ
public class MemberSaveControllerV3 implements ControllerV3 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public ModelView process(Map<String, String> paramMap) {
String username = paramMap.get("username");
int age = Integer.parseInt(paramMap.get("age"));
Member member = new Member(username, age);
memberRepository.save(member);
ModelView mv = new ModelView("save-result");
mv.getModel().put("member", member);
return mv;
}
}
- ํ๋ผ๋ฏธํฐ ์ ๋ณด๋ map์ ๋ด๊ฒจ์๊ธฐ ๋๋ฌธ์ map์์ ํ์ ํ๋ผ๋ฏธํฐ ์กฐํํ๋ฉด ๋จ
- ๋ทฐ์ ํ์ํ ๊ฐ์ฒด๋ map์ ๋ด์์ ๋ฐํ
ํ์ ๋ชฉ๋ก
public class MemberListControllerV3 implements ControllerV3 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public ModelView process(Map<String, String> paramMap) {
List<Member> members = memberRepository.findAll();
ModelView mv = new ModelView("members");
mv.getModel().put("members", members);
return mv;
}
}
๋จผ์ MyView ์์
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);
}
public void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
modelToRequestAttribute(model, request);
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
private void modelToRequestAttribute(Map<String, Object> model, HttpServletRequest request) {
model.forEach((key, value) -> request.setAttribute(key, value));
}
}
ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ
@WebServlet(name = "frontControllerServletV3", urlPatterns = "/front-controller/v3/*")
public class FrontControllerServletV3 extends HttpServlet {
private Map<String, ControllerV3> controllerMap = new HashMap<>();
public FrontControllerServletV3() {
controllerMap.put("/front-controller/v3/members/new-form", new MemberFormControllerV3());
controllerMap.put("/front-controller/v3/members/save", new MemberSaveControllerV3());
controllerMap.put("/front-controller/v3/members", new MemberListControllerV3());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String requestURI = request.getRequestURI();
ControllerV3 controller = controllerMap.get(requestURI);
if (controller == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
Map<String, String> paramMap = createParamMap(request);
ModelView mv = controller.process(paramMap);
String viewName = mv.getViewName();
MyView view = viewResolver(viewName);
view.render(mv.getModel(), request, response);
}
private Map<String, String> createParamMap(HttpServletRequest request) {
Map<String, String> paramMap = new HashMap<>();
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> paramMap.put(paramName, request.getParameter(paramName)));
return paramMap;
}
private MyView viewResolver(String viewName) {
return new MyView("/WEB-INF/views/" + viewName + ".jsp");
}
}
createParamMap()
HttpServletRequest์์ ํ๋ผ๋ฏธํฐ ์ ๋ณด๋ฅผ ๊บผ๋ด์ Map์ผ๋ก ๋ณํํ๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋น Map( paramMap )์ ์ปจํธ๋กค๋ฌ์ ์ ๋ฌํ๋ฉด์ ํธ์ถํ๋ค. |
๋ทฐ ๋ฆฌ์กธ๋ฒ
์ปจํธ๋กค๋ฌ๊ฐ ๋ฐํํ ๋
ผ๋ฆฌ ๋ทฐ ์ด๋ฆ์ ์ค์ ๋ฌผ๋ฆฌ ๋ทฐ ๊ฒฝ๋ก๋ก ๋ณ๊ฒฝ ๊ทธ๋ฆฌ๊ณ ์ค์ ๋ฌผ๋ฆฌ ๊ฒฝ๋ก๊ฐ ์๋ MyView ๊ฐ์ฒด๋ฅผ ๋ฐํ ๋ ผ๋ฆฌ ๋ทฐ ์ด๋ฆ: members ๋ฌผ๋ฆฌ ๋ทฐ ๊ฒฝ๋ก: /WEB-INF/views/members.jsp |
๋ทฐ ๋ ๋
๋ทฐ ๊ฐ์ฒด๋ฅผ ํตํด์ HTML ํ๋ฉด์ ๋ ๋๋ง ํ๋ค.
๋ทฐ ๊ฐ์ฒด์ render() ๋ ๋ชจ๋ธ ์ ๋ณด๋ ํจ๊ป ๋ฐ๋๋ค. JSP๋ request.getAttribute() ๋ก ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ธฐ ๋๋ฌธ์, ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์ request.setAttribute() ๋ก ๋ด์๋๋ค. JSP๋ก ํฌ์๋ ํด์ JSP๋ฅผ ๋ ๋๋ง ํ๋ค. |
5. ๋จ์ํ๊ณ ์ค์ฉ์ ์ธ ์ปจํธ๋กค๋ฌ - v4
ModelView ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฐํํ๋ ๋ถ๋ถ ๋ฒ๊ฑฐ๋ก์-> ์ค์ ๊ฐ๋ฐ ์ ๋จ์, ํธ๋ฆฌ ์ฌ์ฉ ์ค์(์ค์ฉ์ฑ)
ํธ๋ฆฌํ๊ฒ ๊ฐ๋ฐํ ์ ์๋ v4
v4 - v3์์ ๊ฐ์ ๋ ์
- ModelView๋ฅผ ๋ฐํํ์ง ์๊ณ ViewName ๋ฐํ
ControllerV4
public interface ControllerV4 {
/**
* @param paramMap
* @param model
* @return viewName
*/
String process(Map<String, String> paramMap, Map<String, Object> model);
}
- ์ธํฐํ์ด์ค์ ModelView๊ฐ ์์ -> ModelView๊ฐ ์๋ String(View์ด๋ฆ)๋ง ๋ฐํ
ํ์ ์ ์ฅ
public class MemberSaveControllerV4 implements ControllerV4 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public String process(Map<String, String> paramMap, Map<String, Object> model) {
String username = paramMap.get("username");
int age = Integer.parseInt(paramMap.get("age"));
Member member = new Member(username, age);
memberRepository.save(member);
model.put("member", member);
return "save-result";
}
}
* model์ ๋ด๊ธฐ๋งํ๋ฉด ๊ทธ๋๋ก ๋จ์์์ด์ ๋ฐํํ ํ์๊ฐ ์๋ค
* view์ด๋ฆ๋ง ๋ฐํ
@WebServlet(name = "frontControllerServletV4", urlPatterns = "/front-controller/v4/*")
public class FrontControllerServletV4 extends HttpServlet {
private Map<String, ControllerV4> controllerMap = new HashMap<>();
public FrontControllerServletV4() {
controllerMap.put("/front-controller/v4/members/new-form", new MemberFormControllerV4());
controllerMap.put("/front-controller/v4/members/save", new MemberSaveControllerV4());
controllerMap.put("/front-controller/v4/members", new MemberListControllerV4());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String requestURI = request.getRequestURI();
ControllerV4 controller = controllerMap.get(requestURI);
if (controller == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
Map<String, String> paramMap = createParamMap(request);
Map<String, Object> model = new HashMap<>(); //์ถ๊ฐ
String viewName = controller.process(paramMap, model);
MyView view = viewResolver(viewName);
view.render(model, request, response);
}
private Map<String, String> createParamMap(HttpServletRequest request) {
Map<String, String> paramMap = new HashMap<>();
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> paramMap.put(paramName,
request.getParameter(paramName)));
return paramMap;
}
private MyView viewResolver(String viewName) {
return new MyView("/WEB-INF/views/" + viewName + ".jsp");
}
}
- frontcontroller๋ ์ด์ ๋ฒ์ ๊ณผ ๋์ผ (process์ model ์ถ๊ฐ๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ ๋ง๊ณ ๋)
6. ์ ์ฐํ ์ปจํธ๋กค๋ฌ
์ด๋ค ๊ฐ๋ฐ์๋ V3๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํ๊ณ ์ถ๊ณ , ์ด๋ค ๊ฐ๋ฐ์๋ V4๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํ๊ณ ์ถ๋ค๋ฉด?
public interface ControllerV3 {
ModelView process(Map<String, String> paramMap);
}
public interface ControllerV4 {
String process(Map<String, String> paramMap, Map<String, Object> model);
}
์ด๋ํฐ ํจํด
- ๋ ์ปจํธ๋กค๋ฌ๋ ํธํ์ด ๋ถ๊ฐ
-> ์ด๋ํฐ ํจํด์ ์ฌ์ฉํด ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ๋ค์ํ ๋ฐฉ์์ ์ปจํธ๋กค๋ฌ ์ฒ๋ฆฌํ ์ ์๋๋ก ๋ณ๊ฒฝํด์ผํจ
V5
ํธ๋ค๋ฌ ์ด๋ํฐ : ๋ค์ํ ์ข ๋ฅ์ ์ปจํธ๋กค๋ฌ ํธ์ถ ๊ฐ๋ฅ
ํธ๋ค๋ฌ : ์ปจํธ๋กค๋ฌ ๋ฟ๋ง ์๋๋ผ ํด๋นํ๋ ์ข ๋ฅ์ ์ด๋ํฐ๋ง ์๋ค๋ฉด ๋ชจ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ
MyHandlerAdapter
public interface MyHandlerAdapter {
boolean supports(Object handler);
ModelView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException;
}
boolean supports (Object handler)
- handler๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๋งํจ
- ์ด๋ํฐ๊ฐ ํด๋น ์ปจํธ๋กค๋ฌ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋์ง ํ๋จํ๋ ๋ฉ์๋
ModelView handle()
- ์ด๋ํฐ๋ ์ค์ ์ปจํธ๋กค๋ฌ๋ฅผ ํธ์ถํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ก ModelView ๋ฐํํด์ผํจ
- ์ค์ ์ปจํธ๋กค๋ฌ๊ฐ ModelView ๋ฐํํ์ง ๋ชปํ๋ฉด ์ด๋ํฐ๊ฐ ModelView๋ฅผ ์ง์ ์์ฑํด์๋ผ๋ ๋ฐํํด์ผํจ
- ์ด์ ์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ์ค์ ์ปจํธ๋กค๋ฌ ํธ์ถํ์ง๋ง ์ด์ ์ด ์ด๋ํฐ๋ฅผ ํตํด ์ค์ ์ปจํธ๋กค๋ฌ๊ฐ ํธ์ถ๋จ
ControllerV3HandlerAdapter
public class ControllerV3HandlerAdapter implements MyHandlerAdapter {
@Override
public boolean supports(Object handler) { // controllerV3 ์ฒ๋ฆฌํ๋ ์ด๋ํฐ
return (handler instanceof ControllerV3);
}
@Override
public ModelView handle(HttpServletRequest request, HttpServletResponse response, Object handler) {
ControllerV3 controller = (ControllerV3) handler; // ํ๋ณํ (ํ์
๋ณํ supports๋ก ๊ฑธ๋ฌ์ง๋ฏ๋ก ์๋จ)
Map<String, String> paramMap = createParamMap(request);
ModelView mv = controller.process(paramMap);
return mv;
}
private Map<String, String> createParamMap(HttpServletRequest request) {
Map<String, String> paramMap = new HashMap<>();
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> paramMap.put(paramName,
request.getParameter(paramName)));
return paramMap;
}
}
FrontControllerServletV5
@WebServlet(name = "frontControllerServletV5", urlPatterns = "/front-controller/v5/*")
public class FrontControllerServletV5 extends HttpServlet {
private final Map<String, Object> handlerMappingMap = new HashMap<>();
private final List<MyHandlerAdapter> handlerAdapters = new ArrayList<>();
public FrontControllerServletV5() { // ์์ฑ์
initHandlerMappingMap(); // ํธ๋ค๋ฌ ๋งตํ ์ด๊ธฐํ
initHandlerAdapters(); // ์ด๋ํฐ์ด๊ธฐํ
}
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());
}
private void initHandlerAdapters() {
handlerAdapters.add(new ControllerV3HandlerAdapter());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object handler = getHandler(request);
if (handler == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
MyHandlerAdapter adapter = getHandlerAdapter(handler);
ModelView mv = adapter.handle(request, response, handler);
MyView view = viewResolver(mv.getViewName());
view.render(mv.getModel(), request, response);
}
private Object getHandler(HttpServletRequest request) {
String requestURI = request.getRequestURI();
return handlerMappingMap.get(requestURI);
}
private MyHandlerAdapter getHandlerAdapter(Object handler) {
for (MyHandlerAdapter adapter : handlerAdapters) {
if (adapter.supports(handler)) {
return adapter;
}
}
throw new IllegalArgumentException("handler adapter๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. handler=" + handler);
}
private MyView viewResolver(String viewName) {
return new MyView("/WEB-INF/views/" + viewName + ".jsp");
}
}
์ปจํธ๋กค๋ฌ <-> ํธ๋ค๋ฌ
์ด๋ค ๊ฒ์ด๋ผ๋ URL์ ๋งตํ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ํธ๋ค๋ฌ
V6
์ฌ๊ธฐ์ ControllerV4 ์ฌ์ฉํ ์ ์๋๋ก ๊ธฐ๋ฅ ์ถ๊ฐ
@WebServlet(name = "frontControllerServletV6", urlPatterns = "/front-controller/v6/*")
public class FrontControllerServletV6 extends HttpServlet {
private final Map<String, Object> handlerMappingMap = new HashMap<>();
private final List<MyHandlerAdapter> handlerAdapters = new ArrayList<>();
public FrontControllerServletV5() { // ์์ฑ์
initHandlerMappingMap(); // ํธ๋ค๋ฌ ๋งตํ ์ด๊ธฐํ
initHandlerAdapters(); // ์ด๋ํฐ์ด๊ธฐํ
}
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());
// V4์ถ๊ฐ
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()); //V4 ์ถ๊ฐ
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object handler = getHandler(request);
if (handler == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
MyHandlerAdapter adapter = getHandlerAdapter(handler);
ModelView mv = adapter.handle(request, response, handler);
MyView view = viewResolver(mv.getViewName());
view.render(mv.getModel(), request, response);
}
private Object getHandler(HttpServletRequest request) {
String requestURI = request.getRequestURI();
return handlerMappingMap.get(requestURI);
}
private MyHandlerAdapter getHandlerAdapter(Object handler) {
for (MyHandlerAdapter adapter : handlerAdapters) {
if (adapter.supports(handler)) {
return adapter;
}
}
throw new IllegalArgumentException("handler adapter๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. handler=" + handler);
}
private MyView viewResolver(String viewName) {
return new MyView("/WEB-INF/views/" + viewName + ".jsp");
}
}
ControllerV4HandlerAdapter
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) {
ControllerV4 controller = (ControllerV4) handler;
Map<String, String> paramMap = createParamMap(request);
Map<String, Object> model = new HashMap<>();
String viewName = controller.process(paramMap, model);
ModelView mv = new ModelView(viewName);
mv.setModel(model);
return mv;
}
private Map<String, String> createParamMap(HttpServletRequest request) {
Map<String, String> paramMap = new HashMap<>();
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> paramMap.put(paramName,
request.getParameter(paramName)));
return paramMap;
}
}
ControllerV4๋ ๋ทฐ์ ์ด๋ฆ ๋ฐํ -> ์ด๋ํฐ๋ ๋ทฐ์ ์ด๋ฆ์ด ์๋๋ผ ModelView๋ฅผ ๋ง๋ค์ด์ ๋ฐํํด์ผํจ
-> ModelView ํ์์ผ๋ก ๋ฐํํด์ค
'๐จโ๐ป Web Development > Spirng - MVC ํจํด l' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] 6.์คํ๋ง MVC - ๊ธฐ๋ณธ ๊ธฐ๋ฅ (0) | 2023.03.15 |
---|---|
[Spring] 5.์คํ๋ง MVC - ๊ตฌ์กฐ ์ดํด (0) | 2023.03.14 |
[Spring] 3.์๋ธ๋ฆฟ, JSP, MVC ํจํด (0) | 2023.03.13 |
[Spring] 2.์๋ธ๋ฆฟ (0) | 2023.03.12 |
[Spring] 1.์น ์ ํ๋ฆฌ์ผ์ด์ ์ดํด (0) | 2023.03.11 |
์ต๊ทผ๋๊ธ