๐ค์ปจํธ๋กค๋ฌ(Controller - Servlet) : ํด๋ผ์ด์ธํธ์ ๋ชจ๋ ์์ฒญ์ ๋ฐ์ ๋ชจ๋ธ(Model - Class) ์ญํ ์ ๊ฐ์ฒด๋ก ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ฒ๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ๋ทฐ(View - JSP)์๊ฒ ์ ๊ณตํ์ฌ ์๋ต๋๋๋ก ํ๋ก๊ทธ๋จ์ ํ๋ฆ์ ์ ์ดํ๋ ์นํ๋ก๊ทธ๋จ(์๋ธ๋ฆฟ)
โซ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ์ ๋์ ๋ฐฉ์โซ
1.ํด๋ผ์ด์ธํธ์ ๋ชจ๋ ์์ฒญ์ ๋ฐ์ ์ ์๋๋ก ์๋ธ๋ฆฟ์ URL ํจํด์ ์ค์ ํ์ฌ ๋จ์ผ ์ง์ ์ ์ ๊ธฐ๋ฅ ๊ตฌํ
=> Front Controller Pattern
๋ฐฉ๋ฒ1) @WebServlet("url") ์ด๋ ธํ ์ด์ ์ฌ์ฉ
@WebServlet("url") : ํด๋์ค๋ฅผ ์๋ธ๋ฆฟ(์นํ๋ก๊ทธ๋จ)์ผ๋ก ๋ฑ๋กํ๊ณ ์์ฒญ URL ์ฃผ์๋ฅผ ๋งคํํ๋ ์ด๋ ธํ ์ด์
=> ๋งคํ ์ค์ ๋ URL ์ฃผ์์ ํจํด๋ฌธ์(* : ์ ์ฒด, ? : ๋ฌธ์ ํ๋)๋ฅผ ์ฌ์ฉํ์ฌ URL ํจํด ๋ฑ๋ก ๊ฐ๋ฅ
=> @WebServlet("*.do") : ํด๋ผ์ด์ธํธ๊ฐ [XXX.do] ํ์์ URL ์ฃผ์๋ก ์์ฒญํ ๊ฒฝ์ฐ ์๋ธ๋ฆฟ ์คํ
@WebServlet("*.do")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
//์๋ต
๋ฐฉ๋ฒ2) [web.xml] ํ์ผ์ ์๋ฆฌ๋จผํธ๋ฅผ ์ฌ์ฉ
@WebServlet ์ด๋ ธํ ์ด์ ๋์ [web.xml] ํ์ผ์ ์๋ฆฌ๋จผํธ๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค๋ฅผ ์๋ธ๋ฆฟ์ผ๋ก ๋ฑ๋กํ๊ณ URL ํจํด์ ๋งค์นญ ์ฒ๋ฆฌ ๊ฐ๋ฅ
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>mvc</display-name>
<!-- servlet : ํด๋์ค๋ฅผ ์๋ธ๋ฆฟ์ผ๋ก ๋ฑ๋กํ๊ธฐ ์ํ ์๋ฆฌ๋จผํธ -->
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>xyz.itwill.mvc.ControllerServlet</servlet-class>
</servlet>
<!-- servlet-mapping : ์๋ธ๋ฆฟ์ URL ํจํด์ ๋ฑ๋กํ๊ธฐ ์ํ ์๋ฆฌ๋จผํธ -->
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
2.ํด๋ผ์ด์ธํธ์ ์์ฒญ ๋ถ์ : ์์ฒญ URL ์ฃผ์ ์ด์ฉ - http://localhost:8000/mvc/XXX.do
=> HttpServlet ํด๋์ค์ service ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋
//์๋ต
//ํด๋ผ์ด์ธํธ๊ฐ ์๋ธ๋ฆฟ์ ์์ฒญํ ๋๋ง๋ค ์๋ ํธ์ถ๋๋ ๋ฉ์๋
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//2.ํด๋ผ์ด์ธํธ์ ์์ฒญ ๋ถ์ : ์์ฒญ URL ์ฃผ์ ์ด์ฉ - http://localhost:8000/mvc/XXX.do
//HttpServletRequest.getRequestURI() : ์์ฒญ URL ์ฃผ์์์ URI ์ฃผ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
String requestURI=request.getRequestURI();
//System.out.println("requestURI = "+requestURI);//requestURI = /mvc/XXX.do
//HttpServletRequest.getContextPath() : ์์ฒญ URL ์ฃผ์์์ ์ปจํ
์คํธ ๊ฒฝ๋ก๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
String contentPath=request.getContextPath();
//System.out.println("contentPath = "+contentPath);//contentPath = /mvc
String command=requestURI.substring(contentPath.length());
//System.out.println("command = "+command);//command = /XXX.do
//์๋ต
3.๋ชจ๋ธ(Model) ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ํธ์ถํด ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ๋ทฐ(View) ๊ด๋ จ ์ ๋ณด๋ฅผ ๋ฐํ๋ฐ์ ์ ์ฅ
=> ํ๋์ ์์ฒญ์ ๋ํด ํ๋์ ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ ์์ฒญ์ ์ฒ๋ฆฌํ๋๋ก ์ค์ - Command Controller Pattern
=> ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ์ ์ธ๋ ๋ชจ๋ธ ์ญํ ์ ํด๋์ค ์์ฑ - ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ์์ฑ
ํ์๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์์ ํด๋ฆฌ์ด์ธํธ ์์ฒญ(Command)์ ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ ๋์๋๋๋ก ์ค์
์ผ์ชฝ ํจํด์ URL ์ฃผ์๋ฅผ ์ด์ฉํด ์ค๋ฅธ์ชฝ ํด๋์ค ํธ์ถ
=> [/loginform.do] - LoginFormModel ํด๋์ค
=> [/login.do] - LoginModel ํด๋์ค
=> [/logout.do] - LogoutModel ํด๋์ค
=> [/writeform.do] - WriteFormModel ํด๋์ค
=> [/write.do] - WriteModel ํด๋์ค
=> [/list.do] - ListModel ํด๋์ค
=> [/view.do] - ViewModel ํด๋์ค
=> [/modifyform.do] - ModifyFormModel ํด๋์ค
=> [/modify.do] - ModifyModel ํด๋์ค
=> [/remove.do] - RemoveModel ํด๋์ค
=> [/error.do] - ErrorModel ํด๋์ค
//์๋ต
//์๋ ๋ฐฉ๋ฒ๋ณด๋ค Map๊ฐ์ฒด ํ์ฉํ๋ ๋ฐฉ๋ฒ์ด ๋ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง.but ์ง๊ธ์ ์ด ๋ฐฉ๋ฒ์ผ๋ก ์์ฑ.
//๋ชจ๋ธ ์ญํ ์ ํด๋์ค๊ฐ ์์๋ฐ๊ธฐ ์ํ ์ธํฐํ์ด์ค๋ก ์ฐธ์กฐ๋ณ์ ์์ฑ
// => ์ธํฐํ์ด์ค๋ก ์์ฑ๋ ์ฐธ์กฐ๋ณ์์๋ ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ๋ชจ๋ ์์ํด๋์ค(๋ชจ๋ธ) ๊ฐ์ฒด ์ ์ฅ ๊ฐ๋ฅ
Action action=null;
//ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ณด๋ฅผ ๋น๊ตํ์ฌ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋ชจ๋ธ ์ญํ ์ ํด๋์ค๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ
//์ธํฐํ์ด์ค ์ฐธ์กฐ๋ณ์์ ์ ์ฅ
if(command.equals("/loginform.do")) {
action=new LoginFormModel();
} else if(command.equals("/login.do")) {
action=new LoginModel();
} else if(command.equals("/logout.do")) {
action=new LogoutModel();
} else if(command.equals("/writeform.do")) {
action=new WriteFormModel();
} else if(command.equals("/write.do")) {
action=new WriteModel();
} else if(command.equals("/list.do")) {
action=new ListModel();
} else if(command.equals("/view.do")) {
action=new ViewModel();
} else if(command.equals("/modifyform.do")) {
action=new ModifyFormModel();
} else if(command.equals("/modify.do")) {
action=new ModifyModel();
} else if(command.equals("/remove.do")) {
action=new RemoveModel();
} else if(command.equals("/error.do")) {
action=new ErrorModel();
} else {//ํด๋ผ์ด์ธํธ ์์ฒญ์ ๋ํ ๋ชจ๋ธ ์ญํ ์ ํด๋์ค๊ฐ ์๋ ๊ฒฝ์ฐ
action=new ErrorModel();
}
//์ธํฐํ์ด์ค ์ฐธ์กฐ๋ณ์๋ก ์ถ์๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฌต์์ ๊ฐ์ฒด ํ๋ณํ์ ์ํด ์ฐธ์กฐ๋ณ์์
//์ ์ฅ๋ ๋ชจ๋ธ ๊ฐ์ฒด์ ์ค๋ฒ๋ผ์ด๋ ์ ์ธ๋ ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ
//๋ทฐ ๊ด๋ จ ์ ๋ณด(ActionForward ๊ฐ์ฒด)๋ฅผ ๋ฐํ๋ฐ์ ์ ์ฅ - ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋์ ์ํ ๋คํ์ฑ
ActionForward actionForward=action.execute(request, response);
//์๋ต
4.๋ทฐ ๊ด๋ จ ์ ๋ณด๊ฐ ์ ์ฅ๋ ActionForward ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ์๋ต ์ฒ๋ฆฌ
//์๋ต
if(actionForward.isForward()) {//ActionForward ๊ฐ์ฒด์ forward ํ๋๊ฐ์ด [true]์ธ ๊ฒฝ์ฐ
//JSP ๋ฌธ์๋ก ํฌ์๋ ์ด๋ํ์ฌ JSP ๋ฌธ์์ ์คํ๊ฒฐ๊ณผ(HTML)๋ก ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต ์ฒ๋ฆฌ
request.getRequestDispatcher(actionForward.getPath()).forward(request, response);
} else {//ActionForward ๊ฐ์ฒด์ forward ํ๋๊ฐ์ด [false]์ธ ๊ฒฝ์ฐ
//์๋ธ๋ฆฟ(์ปจํธ๋กค๋ฌ)์์ URL ์ฃผ์(/XXX.do)๋ก ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต ์ฒ๋ฆฌ
response.sendRedirect(actionForward.getPath());
}
}
}
MVCํจํด์ ํ๊ณ๋ฅผ ์ ์ค๋ช ํด์คโฌ๏ธ
https://catsbi.oopy.io/441b4af6-e877-4dc5-9695-2983bbe22799
์๋ธ๋ฆฟ,JSP, MVCํจํด
๋ชฉ์ฐจ
catsbi.oopy.io
'Backend > Spring Framework' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring]Spring AOP ๊ฐ๋ ๋ฐ ํ์ฉ ๋ฐฉ๋ฒ (0) | 2024.06.25 |
---|---|
MVC1 ํจํด vs MVC2 ํจํด(+Spring MVC2 ํจํด) (0) | 2024.02.26 |