JSP| ๐MVC ํจํด์ ์ด์ฉํ ๊ฒ์ํ
- Model(๋๋ถ๋ถ java): DB์์ ์ถ์ถ์ด๋ ์์ ํ์ฌ Controller
- View (๋๋ถ๋ถ JSPํ์ผ): ํ๋ฉด,UI
- Controller: ์์ฒญ์ ๋ด๋น
Model 1 : (JSP)view+controller
- ์๊ฐ์ด ์์ ๋, ๋ง๋๋ ๊ฒ ๊ธ์ ๋ฌด: 10๋
์ , ์์ฆ ์ ์ฌ์ฉํ์ง ์์
Model 2 : M + V + C
- ๋ชจ๋ํ: ๋ถํํ
- ๊ฐ๋ฐํ๋ฉด ํ ์๋ก ๋ชจ๋ํ๊ฐ ์ค์
์ ์ฒด์ ์ธ ์ปดํผ๋ํธ ์ค๊ณ
1. Front Controller์์ ๋จผ์ ๋ฐ๋๋ค
2. ํน์ ์ปค๋งจ๋ ํธ์ถ
3. ์ปค๋งจ๋๊ฐ DAO์ ์ ํฉํ ๋ฉ์๋ ํธ์ถ
4. ๊ฒฐ๊ณผ๋ฌผ: DTO ํํ
5.
๊ฒ์ํ ๋ง๋ค๊ธฐ
DataBase ์์ฑ
Front Controller ๋ง๋ค๊ธฐ
/*
http://localhost:8080/Book/example.do๋ผ๋
request๊ฐ ๋ค์ด์๋ค๊ณ ๊ฐ์ ํ ๋
*/
//command: ์ด๋ค ๋ก์ง์ ์ํํ ๊ฒ ์ธ๊ฐ
BCommand command = null;
String uri = request.getRequestURI();// /Book/example.do
String conPath = request.getContextPath();// /Book
//conPath.length๋งํผ ์งค๋ผ๋ด์ ๋ท๋ถ๋ถ์ ํ์ฅ์๋ช
์ ์ป๋๋ค
String com = uri.substring(conPath.length()); // /example.do
package com.javalec.ex.frontcontroller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javalec.ex.command.BCommand;
import com.javalec.ex.command.BContentCommand;
import com.javalec.ex.command.BDeleteCommand;
import com.javalec.ex.command.BListCommand;
import com.javalec.ex.command.BModifyCommand;
import com.javalec.ex.command.BReplyCommand;
import com.javalec.ex.command.BReplyViewCommand;
import com.javalec.ex.command.BWriteCommand;
//ํ์ฅ์๋ช
์ผ๋ก url mapping
@WebServlet("*.do")
// public class BFrontController extends HttpServlet: ์๋ธ๋ฆฟ > ๊ธฐ๋ณธ์ผ๋ก doGet doPost๋ฅผ ๊ฐ์ง๋ค
public class BFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BFrontController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
actionDo(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("actionDo");
request.setCharacterEncoding("EUC-KR");
//viewPage: ์์ฒญ์ ๋ฐ์ ๋ค์ ๋ณด์ฌ์ค ํ์ด์ง
String viewPage = null;
//command: ์ด๋ค ๋ก์ง์ ์ํํ ๊ฒ ์ธ๊ฐ
BCommand command = null;
String uri = request.getRequestURI();
String conPath = request.getContextPath();
//conPath.length๋งํผ ์งค๋ผ๋ด์ ๋ท๋ถ๋ถ์ ํ์ฅ์๋ช
์ ์ป๋๋ค
String com = uri.substring(conPath.length());
// ์ป์ ํ์ฅ์๋ช
์ ๋ฐ๋ผ์ ์๋๋ฅผ ์ํ
if(com.equals("/write_view.do")) {
viewPage = "write_view.jsp";
} else if(com.equals("/write.do")) {
command = new BWriteCommand();
command.execute(request, response);
viewPage = "list.do";
} else if(com.equals("/list.do")) {
command = new BListCommand();
command.execute(request, response);
viewPage = "list.jsp";
} else if(com.equals("/content_view.do")){
command = new BContentCommand();
command.execute(request, response);
viewPage = "content_view.jsp";
} else if(com.equals("/modify.do")) {
command = new BModifyCommand();
command.execute(request, response);
viewPage = "list.do";
} else if(com.equals("/delete.do")) {
command = new BDeleteCommand();
command.execute(request, response);
viewPage = "list.do";
} else if(com.equals("/reply_view.do")) {
command = new BReplyViewCommand();
command.execute(request, response);
viewPage = "reply_view.jsp";
} else if(com.equals("/reply.do")) {
command = new BReplyCommand();
command.execute(request, response);
viewPage = "list.do";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
}
}
Command ๋ง๋ค๊ธฐ
- BCommand.java: ์ธํฐํ์ด์ค๋ก request, response๋ฅผ ์ธ์๋ก ๋๊ณ ๋ค๋ฅธ Command๋ค์ด override ํ๋๋ก ํ๋ค >> request, response ์ํํ๋ ๋ก์ง์ ๊ฐ์ ์์์
package com.javalec.ex.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface BCommand {
void execute(HttpServletRequest request, HttpServletResponse response);
// ํ๋ผ๋ฏธํฐ 2: request, response >>> ๋ค๋ฅธ Command ํด๋์ค์์ override
}
- BContentCommand
package com.javalec.ex.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javalec.ex.dao.BDao;
import com.javalec.ex.dto.BDto;
public class BContentCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String bId = request.getParameter("bId");
BDao dao = new BDao();
BDto dto = dao.contentView(bId);
request.setAttribute("content_view", dto);
}
}
- BDeleteCommand
package com.javalec.ex.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javalec.ex.dao.BDao;
public class BDeleteCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String bId = request.getParameter("bId");
BDao dao = new BDao();
dao.delete(bId);
}
}
DTO(Data Transfer Object)
- DB์ ๋ฐ์ดํฐ๋ฅผ ์๋ฐ ๊ฐ์ฒด๋ก ์ฌ์ฉํ ์ ์๊ฒ
package com.javalec.ex.dto;
import java.sql.Timestamp;
public class BDto {
int bId;
String bName;
String bTitle;
String bContent;
Timestamp bDate;
int bHit;
int bGroup;
int bStep;
int bIndent;
public BDto() {
// TODO Auto-generated constructor stub
}
public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit, int bGroup, int bStep, int bIndent) {
// TODO Auto-generated constructor stub
this.bId = bId;
this.bName = bName;
this.bTitle = bTitle;
this.bContent = bContent;
this.bDate = bDate;
this.bHit = bHit;
this.bGroup = bGroup;
this.bStep = bStep;
this.bIndent = bIndent;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public String getbTitle() {
return bTitle;
}
public void setbTitle(String bTitle) {
this.bTitle = bTitle;
}
public String getbContent() {
return bContent;
}
public void setbContent(String bContent) {
this.bContent = bContent;
}
public Timestamp getbDate() {
return bDate;
}
public void setbDate(Timestamp bDate) {
this.bDate = bDate;
}
public int getbHit() {
return bHit;
}
public void setbHit(int bHit) {
this.bHit = bHit;
}
public int getbGroup() {
return bGroup;
}
public void setbGroup(int bGroup) {
this.bGroup = bGroup;
}
public int getbStep() {
return bStep;
}
public void setbStep(int bStep) {
this.bStep = bStep;
}
public int getbIndent() {
return bIndent;
}
public void setbIndent(int bIndent) {
this.bIndent = bIndent;
}
}
DAO(Data Access Object)
view ํ์ด์ง ๋ง๋ค๊ธฐ
๊ธ ๋ชฉ๋ก ํ์ด์ง ๋ง๋ค๊ธฐ(Read)
๊ธ ๋ด์ฉ ๋ณด๊ธฐ ํ์ด์ง ๋ง๋ค๊ธฐ(Read)
๊ธ ๋ด์ฉ ์์ ํ์ด์ง ๋ง๋ค๊ธฐ(Update)
๊ธ ์ญ์ ํ์ด์ง ๋ง๋ค๊ธฐ(Delete)
- ํ์ ์ธ์ฆ ์ ์ฐจ ์๋ต๋จ
๊ธ ๋ต๋ณ ๋ฌ๊ธฐ
- ๊ทธ๋ฃน๋๋ฒ: ์๊ธ๊ณผ ๋ต๋ณ๊ธ์ ๋ชจ๋ ๋ฌถ์ด์(์๊ธ์ด 3์ด๋ฉด ๋ต๋ณ 1,2,3...๋ 3์ ๊ฐ์ ๊ฐ์ง๋ค)
- ์คํ : ์๊ธ์์ ์์ํด์ ๋ต๋ณ์ด ๋ช๋ฒ์งธ ์๋๋ก ๋ฌ๋ฆฌ ๊ฒ ์ธ๊ฐ.
- ์ธ๋ดํธ: ์ผ๋ง๋งํผ ์์ผ๋ก ๋ค์ฌ์จ์ ํ๊ธฐํ ๊ฒ์ธ๊ฐ
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(JSP| ๐MVC ํจํด์ ์ด์ฉํ ๊ฒ์ํ), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@kji306301/JSP-MVC-ํจํด์-์ด์ฉํ-๊ฒ์ํ์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค