MVC
Last updated
Last updated
Model, View, Controller를 뜻하는 용어로 개발 형태의 일종이다.
Model은 데이터베이스와의 관계를 담당한다. 클라이언트 요청에 필요한 자료를 데이터베이스로부터 추출하거나, 수정하여 Contoller로 전달한다.
View는 사용자한테 보여지는 UI화면이다. 주로 .jsp
파일로 작성하며, Controller에서 어떤 View 컴포넌트를 보여줄지 결정한다.
Controller는 클라이언트의 요청을 받고, 적절한 Model에 지시를 내리며, Model에서 전달된 데이터를 적절한 View에 전달한다.
작업을 분할하여, 추후 유지보수를 더 좋게 만든다.
MVC에서 View와 Controller 같이 있는 형태이다. 한 파일에 같이 있다고 생각하면된다.
클라이언트 -> JSP(View + Controller) -> DAO(Model) -> DB
이전에 많이 쓰이던 방법이다.
전통적인 MVC를 따르는 방법이다. Model, View, Controller가 모두 모듈화 되어 있는 형태이다.
클라이언트 -> Servlet(Controller)<->Command&DAO(Model)<->DB
ㄴ> JSP
클라이언트의 요청을 받는 FrontController를 만들자. 사용자의 요청을 집중시킨다.
package controller;
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 command.BCommand;
import command.BContentCommand;
import command.BDeleteCommand;
import command.BListCommand;
import command.BModifyCommand;
import command.BReplyCommand;
import command.BReplyViewCommand;
import command.BWriteCommand;
/**
* Servlet implementation class BFrontController
*/
@WebServlet("*.do")
public class BFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BFrontController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("FrontController doGet");
actionDo(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("FrontController doPost");
actionDo(request,response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
System.out.println("Frontcontroller actionDo");
request.setCharacterEncoding("UTF-8");
String viewPage = null;
BCommand command = null;
String uri = request.getRequestURI();
String conPath = request.getContextPath();
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";
}
// forwarding
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
}
}
Command 인터페이스를 만들어서 각각의 클래스를 만들어준다.
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface BCommand {
void execute(HttpServletRequest request, HttpServletResponse response);
}
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
public class BWriteCommand implements BCommand{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDao dao = new BDao();
dao.write(bName, bTitle, bContent);
}
}
package command;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
import dto.BDto;
public class BListCommand implements BCommand{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
BDao dao = new BDao();
ArrayList<BDto> dtos = dao.list();
request.setAttribute("list", dtos);
}
}
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
import 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 command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
public class BModifyCommand implements BCommand{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDao dao = new BDao();
dao.modify(bId,bName,bTitle,bContent);
}
}
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import 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);
}
}
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
import dto.BDto;
public class BReplyViewCommand 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.reply_view(bId);
request.setAttribute("reply_view", dto);
}
}
package command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BDao;
public class BReplyCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
String bGroup = request.getParameter("bGroup");
String bStep = request.getParameter("bStep");
String bIndent = request.getParameter("bIndent");
BDao dao = new BDao();
dao.reply(bId,bName, bTitle, bContent, bGroup, bStep, bIndent);
}
}
데이터 베이스의 데이터 DTO 객체를 만든다.
package 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() {
}
public BDto(int bId, String bName,String bTitle, String bContent, Timestamp bDate, int bHit,int bGroup, int bStep, int bIndent) {
this.bId = bId;
this.bName = bName;
this.bContent = bContent;
this.bTitle = bTitle;
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는 DB를 사용해 데이터를 조회하거나 조작하는 기능을 전담하도록 만든 Object이다.
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import dto.BDto;
public class BDao {
DataSource dataSource;
public BDao() {
try {
Context context = new InitialContext();
dataSource = (DataSource)context.lookup("java:comp/env/jdbc/javaproject");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void write(String bName, String bTitle, String bContent) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "insert into mvc_board ( bName, bTitle, bContent,bDate, bHit, bGroup, bStep, bIndent) select ?, ?, ?, NOW(),0, MAX(bId)+1, 0, 0 from mvc_board";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
public ArrayList<BDto> list(){
ArrayList<BDto> dtos = new ArrayList<BDto>();
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from mvc_board order by bGroup desc, bStep asc";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
BDto dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
dtos.add(dto);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(resultSet!=null)resultSet.close();
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dtos;
}
public BDto contentView(String strID) {
upHit(strID);
BDto dto = null;
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select * from mvc_board where bId = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(strID));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(resultSet!=null)resultSet.close();
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dto;
}
public void modify(String bId,String bName, String bTitle, String bContent) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "update mvc_board set bName = ?, bTitle = ?, bContent = ? where bId = ?";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
preparedStatement.setInt(4, Integer.parseInt(bId));
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
public void delete(String bId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "delete from mvc_board where bId = ?";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(bId));
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
public BDto reply_view(String str) {
BDto dto=null;
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select * from mvc_board where bId = ?";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(str));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
dto = new BDto(bId,bName, bTitle, bContent, bDate,bHit,bGroup, bStep, bIndent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(resultSet!=null)resultSet.close();
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dto;
}
public void reply(String bId, String bName,String bTitle,String bContent,String bGroup,String bStep,String bIndent) {
replyShape(bGroup,bStep);
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "insert into mvc_board ( bName, bTitle, bContent,bDate, bGroup, bStep, bIndent) values ( ?, ?, ?,NOW(), ?, ?, ?)";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
preparedStatement.setInt(4, Integer.parseInt(bGroup));
preparedStatement.setInt(5, Integer.parseInt(bStep)+1);
preparedStatement.setInt(6, Integer.parseInt(bIndent)+1);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
private void replyShape(String strGroup, String strStep) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "update mvc_board set bStep = bStep + 1 where bGroup = ? and bStep > ?";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(strGroup));
preparedStatement.setInt(2, Integer.parseInt(strStep));
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
private void upHit(String bId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "update mvc_board set bHit = bHit + 1 where bId = ?";
preparedStatement = (PreparedStatement) connection.prepareStatement(query);
preparedStatement.setString(1, bId);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(preparedStatement!=null)preparedStatement.close();
if(connection!=null)connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
클라이언트의 요청에 대해서 FrontController에서 작업을 분기하고, 해당 Command클래스가 작동하여 DAO를 이용한 데이터 베이스 작업을 한다.
DAO클래스의 결과물로 DTO객체가 View(.jsp페이지)로 전달되며, View에서는 클라이언트의 요청에 대한 응답으로 화면(UI)를 구성하여 출력 한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<form action="modify.do" method="post">
<input type="hidden" name="bId" value="${content_view.bId}">
<tr>
<td> 번호 </td>
<td> ${content_view.bId} </td>
</tr>
<tr>
<td> 히트 </td>
<td> ${content_view.bHit} </td>
</tr>
<tr>
<td> 이름 </td>
<td> <input type="text" name="bName" value="${content_view.bName}"></td>
</tr>
<tr>
<td> 제목 </td>
<td> <input type="text" name="bTitle" value="${content_view.bTitle}"></td>
</tr>
<tr>
<td> 내용 </td>
<td> <textarea rows="10" name="bContent" >${content_view.bContent}</textarea></td>
</tr>
<tr >
<td colspan="2"> <input type="submit" value="수정"> <a href="list.do">목록보기</a> <a href="delete.do?bId=${content_view.bId}">삭제</a> <a href="reply_view.do?bId=${content_view.bId}">답변</a></td>
</tr>
</form>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>번호</td>
<td>이름</td>
<td>제목</td>
<td>날짜</td>
<td>히트</td>
</tr>
<c:forEach items="${list}" var="dto">
<tr>
<td>${dto.bId}</td>
<td>${dto.bName}</td>
<td>
<c:forEach begin="1" end="${dto.bIndent}">-</c:forEach>
<a href="content_view.do?bId=${dto.bId}">${dto.bTitle}</a></td>
<td>${dto.bDate}</td>
<td>${dto.bHit}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5"> <a href="write_view.do">글작성</a> </td>
</tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<form action="reply.do" method="post">
<input type="hidden" name="bId" value="${reply_view.bId}">
<input type="hidden" name="bGroup" value="${reply_view.bGroup}">
<input type="hidden" name="bStep" value="${reply_view.bStep}">
<input type="hidden" name="bIndent" value="${reply_view.bIndent}">
<tr>
<td> 번호 </td>
<td> ${reply_view.bId} </td>
</tr>
<tr>
<td> 히트 </td>
<td> ${reply_view.bHit} </td>
</tr>
<tr>
<td> 이름 </td>
<td> <input type="text" name="bName" value="${reply_view.bName}"></td>
</tr>
<tr>
<td> 제목 </td>
<td> <input type="text" name="bTitle" value="${reply_view.bTitle}"></td>
</tr>
<tr>
<td> 내용 </td>
<td> <textarea rows="10" name="bContent">${reply_view.bContent}</textarea></td>
</tr>
<tr >
<td colspan="2"><input type="submit" value="답변"> <a href="list.do" >목록</a></td>
</tr>
</form>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<form action="write.do" method="post">
<tr>
<td> 이름 </td>
<td> <input type="text" name="bName" size = "50"> </td>
</tr>
<tr>
<td> 제목 </td>
<td> <input type="text" name="bTitle" size = "50"> </td>
</tr>
<tr>
<td> 내용 </td>
<td> <textarea name="bContent" rows="10" ></textarea> </td>
</tr>
<tr >
<td colspan="2"> <input type="submit" value="제출"> <a href="list.do">리스트보기</a></td>
</tr>
</form>
</table>
</body>
</html>