JSP| 💔MVCモードを使用した掲示板
Model 1 : (JSP)view+controller
Model 2 : M + V + C
ソリッド構成部品設計
1.フロントコントローラから先に受信
2.特定のコマンドを呼び出す
3.DAOに適したメソッドを呼び出す
4.成果物:DTO形式
5.
掲示板の作成
DataBaseの作成
Front Controlの作成
/*
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の作成
DataBaseの作成
Front Controlの作成
/*
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の作成
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
}
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);
}
}
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)
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)
ビューページの作成
記事リストページの作成(Read)
「記事の内容の表示」(Read)ページの作成
記事の変更ページの作成(更新)
削除記事ページの作成(Delete)
文章の答弁
Reference
この問題について(JSP| 💔MVCモードを使用した掲示板), 我々は、より多くの情報をここで見つけました https://velog.io/@kji306301/JSP-MVC-패턴을-이용한-게시판テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol