JSP| 💔MVCモードを使用した掲示板


  • モデル(多くはjava):データベースからコントローラ
  • を抽出または変更する
  • ビュー(大部分はJSPファイル):画面、UI
  • コントローラ:要求担当
  • Model 1 : (JSP)view+controller


  • 時間がないときは、制作が急務です:10年前、最近はあまり使われていません
  • 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の作成


  • BCommand.JAva:インタフェースはrequest、responseをパラメータとし、他のコマンドを上書き>>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データはJavaオブジェクトとして
  • を用いることができる.
    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)


  • 会員認証プログラムは
  • を省略する.

    文章の答弁


  • グループ番号:すべてのコメントと返信記事(3の場合は1、2、3と回答)
  • ステップ:1つの文章から、答えは何位にランクされます.
  • ピン:いくら以内にマークを書くか