📃 MVC (Model, View, Controller) 로 웹 구성하기
Board.java - 객체
package kosta.model;
import java.io.Serializable;
public class Board implements Serializable {
private int seq;
private String title;
private String writer;
private String contents;
private String regdate;
private int hitcount;
public Board() {
}
public Board(int seq, String title, String writer, String contents, String regdate, int hitcount) {
super();
this.seq = seq;
this.title = title;
this.writer = writer;
this.contents = contents;
this.regdate = regdate;
this.hitcount = hitcount;
}
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getRegdate() {
return regdate;
}
public void setRegdate(String regdate) {
this.regdate = regdate;
}
public int getHitcount() {
return hitcount;
}
public void setHitcount(int hitcount) {
this.hitcount = hitcount;
}
@Override
public String toString() {
return "Board [seq=" + seq + ", title=" + title + ", writer=" + writer + ", contents=" + contents + ", regdate="
+ regdate + ", hitcount=" + hitcount + "]";
}
}
BoardDao.java
public class BoardDao2 {
private static BoardDao2 dao = new BoardDao2();
public static BoardDao2 getInstance() {
return dao;
}
public SqlSessionFactory getSqlSessionFactory() {
//mybatis-config.xml => SqlSessionFactory 변환
String resource = "mybatis-config.xml";
InputStream in = null;
try {
in = Resources.getResourceAsStream(resource);
} catch (Exception e) {
e.printStackTrace();
}
return new SqlSessionFactoryBuilder().build(in);
}
public int insertBoard(Board board) {
SqlSession sqlSession = getSqlSessionFactory().openSession();
int re = -1;
try {
//re = sqlSession.insert("kosta.mapper.BoardMapper.insertBoard", board);
re = sqlSession.getMapper(BoardMapper.class).insertBoard(board);
if(re>0) {
sqlSession.commit();
}else {
sqlSession.rollback();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(sqlSession != null) {
sqlSession.close();
}
}
return re;
}
public List<Board> listBoard(){
SqlSession sqlSession = getSqlSessionFactory().openSession();
List<Board> list = null;
try {
//list = sqlSession.selectList("kosta.mapper.BoardMapper.listBoard");
list = sqlSession.getMapper(BoardMapper.class).listBoard();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(sqlSession != null) {
sqlSession.close();
}
}
return list;
}
public Board detailBoard(int seq) {
SqlSession sqlSession = getSqlSessionFactory().openSession();
Board board = null;
try {
board = sqlSession.getMapper(BoardMapper.class).detailBoard(seq);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(sqlSession != null) {
sqlSession.close();
}
}
return board;
}
BoardService.java
package kosta.service;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import kosta.model.Board;
import kosta.model.BoardDao2;
public class BoardService {
private static BoardService service = new BoardService();
private static BoardDao2 dao;
public static BoardService getInstance() {
dao = BoardDao2.getInstance();
return service;
}
public int insertBoardService(HttpServletRequest request)throws Exception {
request.setCharacterEncoding("utf-8");
Board board = new Board();
board.setTitle(request.getParameter("title"));
board.setWriter(request.getParameter("writer"));
board.setContents(request.getParameter("contents"));
return dao.insertBoard(board);
}
public List<Board> listBoardService(HttpServletRequest request)throws Exception{
List<Board> list = dao.listBoard();
return list;
}
public Board detailBoard(int seq)throws Exception {
return dao.detailBoard(seq);
}
}
Controller.java
package kosta.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 kosta.action.Action;
import kosta.action.ActionForward;
import kosta.action.DetailAction;
import kosta.action.InsertAction;
import kosta.action.InsertFormAction;
import kosta.action.ListAction;
@WebServlet("/board/*")
public class MyController extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyController() {
super();
}
public void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//url 식별
String requestURI = request.getRequestURI(); // /MVC/board/list.do
String contextPath = request.getContextPath();
String command = requestURI.substring(contextPath.length()+7);
System.out.println(command);
Action action = null;
ActionForward forward = null;
if(command.equals("insertForm.do")) {
action = new InsertFormAction();
try {
forward = action.execute(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}else if(command.equals("insertAction.do")) {
action = new InsertAction();
try {
forward = action.execute(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}else if(command.equals("listAction.do")) {
action = new ListAction();
try {
forward = action.execute(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}else if(command.equals("detailAction.do")) {
action = new DetailAction();
try {
forward = action.execute(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
if(forward != null) {
if(forward.isRedirect()) { //redirect
response.sendRedirect(forward.getPath());
}else { //Dispatcher
RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
dispatcher.forward(request, response);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}
}