JSP| ๐Ÿ’”MVC ํŒจํ„ด์„ ์ด์šฉํ•œ ๊ฒŒ์‹œํŒ

40865 ๋‹จ์–ด jspMVCMVC

  • 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์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค)
  • ์Šคํ…: ์›๊ธ€์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ต๋ณ€์ด ๋ช‡๋ฒˆ์งธ ์•„๋ž˜๋กœ ๋‹ฌ๋ฆฌ ๊ฒƒ ์ธ๊ฐ€.
  • ์ธ๋ดํŠธ: ์–ผ๋งˆ๋งŒํผ ์•ˆ์œผ๋กœ ๋“ค์—ฌ์จ์„œ ํ‘œ๊ธฐํ•  ๊ฒƒ์ธ๊ฐ€

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ