Matin Functions
1. Reply
2. File upload/download - cos library
3. Request object
4. Controller Class : Java Servlet
5. Model = Service + DAO
6. View UI Interface : EL, JSTL
Structures
Action Interface inheritance
db.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=totoro
jdbc.password=totoro123
model2.sql
select * from tab;
select * from seq;
select * from model22;
create table model22(
board_num number,
board_name varchar2(20),
board_pass varchar2(15),
board_subject varchar2(50),
board_content varchar2(2000),
board_file varchar2(50),
board_re_ref number,
board_re_lev number,
board_re_seq number,
board_readcount number,
board_date timestamp,
primary key(board_num)
);
create sequence model22_seq
start with 1
increment by 1
nocache;
mybatis-config.xml
<typeAlias> <properties> <mappers>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties" />
<typeAliases>
<typeAlias type="model.BoardBean" alias="board"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="board.xml" />
</mappers>
</configuration>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Bulletin Board
<%
response.sendRedirect("./BoardListAction.do");
%>
<script>
// location.href="./BoardListAction.do";
</script>
</body>
</html>
qna_board_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<a href="./BoardForm.do">New Post</a> <br>
Total Posts : ${listcount}
<table border=1 width=700 align=center>
<caption>Posts List</caption>
<tr>
<th>No.</th>
<th>Title</th>
<th>Writer</th>
<th>Date</th>
<th>View</th>
</tr>
<c:set var="num" value="${listcount - (page-1) * 10 }"/>
<c:forEach var="b" items="${boardlist}">
<tr>
<td>
${num}
<c:set var="num" value="${num-1}"/>
</td>
<td>
<c:if test="${b.board_re_lev > 0}">
<c:forEach var="i" begin="0" end="${b.board_re_lev}">
</c:forEach>
</c:if>
<c:if test="${b.board_re_lev > 0}">
Re.
</c:if>
<a href="./BoardDetailAction.do?board_num=${b.board_num}&page=${page}">
${b.board_subject}
</a>
</td>
<td>${b.board_name}</td>
<td>
<fmt:formatDate value="${b.board_date}" pattern="yyyy-MM-dd H:mm"/>
</td>
<td>${b.board_readcount}</td>
</tr>
</c:forEach>
</table><br>
<center>
<c:if test="${listcount > 0}">
<a href="./BoardListAction.do?page=1" style="text-decoration:none"> < </a>
<c:if test="${startPage > 10}">
<a href="./BoardListAction.do?page=${startPage - 10}">[Prev]</a>
</c:if>
<c:forEach var="i" begin="${startPage}" end="${endPage}">
<c:if test="${i == page}">
[${i}]
</c:if>
<c:if test="${i != page}">
<a href="./BoardListAction.do?page=${i}">[${i}]</a>
</c:if>
</c:forEach>
<c:if test="${endPage < pageCount}">
<a href="./BoardListAction.do?page=${startPage + 10}">[Next]</a>
</c:if>
<a href="./BoardListAction.do?page=${pageCount}" style="text-decoration:none"> > </a>
</c:if>
</center>
Controller Class(List)
//List
}else if(command.equals("/BoardListAction.do")) {
try {
action = new BoardListAction();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
BoardListAction.java
Pagination
package service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
public class BoardListAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardListAction");
int page=1;
int limit=10; /
if(request.getParameter("page")!=null) {
page = Integer.parseInt(request.getParameter("page"));
}
int startRow = (page-1) * limit + 1;
int endRow = page * limit;
List boardlist = null;
BoardDAO dao = BoardDAO.getInstance();
int listcount = dao.getCount();
// boardlist = dao.getList(startRow, endRow);
// boardlist = dao.getList(page);
// Map ----------------------------------
Map map = new HashMap();
map.put("start", startRow);
map.put("end", endRow);
boardlist = dao.getList(page);
// boardlist = dao.getList(map);
//-------------------------------------------------
System.out.println("listcount:"+listcount);
System.out.println("boardlist:"+boardlist);
int pageCount = listcount/limit + ((listcount%limit==0) ? 0:1);
int startPage = ((page-1)/10) * limit + 1;
int endPage = startPage + 10 - 1;
if(endPage > pageCount) endPage = pageCount;
request.setAttribute("page", page);
request.setAttribute("listcount", listcount);
request.setAttribute("boardlist", boardlist);
request.setAttribute("pageCount", pageCount);
request.setAttribute("startPage", startPage);
request.setAttribute("endPage", endPage);
//<c:forEach items="${}"> </forEach>
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_list.jsp");
return forward;
}
}
DAO Class(List)
// List
public List getList(int page) throws Exception {
// public List getList(Map map) throws Exception {
List list = new ArrayList();
SqlSession session=getSession();
list = session.selectList("board_list", page);
return list;
}
// View +1
public void updateCount(int board_num) throws Exception{
SqlSession session = getSession();
session.update("board_updatecount", board_num);
}
Mapper Class(Main post)
<!-- page -->
<select id="board_list" parameterType="int" resultType="board">
select * from (select rownum rnum, board.* from (
select * from model22 order by board_re_ref desc,board_re_seq asc) board )
where rnum >= (#{page}-1) * 10 + 1 and rnum <= #{page}*10
</select>
<!-- Map -->
<!-- <select id="board_list" parameterType="Map" resultType="board">
select * from (select rownum rnum, board.* from (
select * from model22 order by board_re_ref desc,board_re_seq asc) board )
where rnum >= #{start} and rnum <= #{end}
</select> -->
qna_board_write.jsp
For the file attachment function, you need to include enctype="multiypart/form-date" in the form tag.
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>MVC Bulletin board with MyBatis</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath() %>/board/script.js"></script>
</head>
<body>
<form action="<%=request.getContextPath() %>/BoardAddAction.do" method="post"
enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" align=center border=1>
<tr align="center" valign="middle">
<td colspan="5">Bulletin Boarc</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Writer</div>
</td>
<td>
<input name="board_name" id="board_name" type="text" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Password</div>
</td>
<td>
<input name="board_pass" id="board_pass" type="password" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Title</div>
</td>
<td>
<input name="board_subject" id="board_subject" type="text" size="50" maxlength="100"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">Content</div>
</td>
<td>
<textarea name="board_content" id="board_content" cols="67" rows="15"></textarea>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">Attach File</div>
</td>
<td>
<input name="board_file" type="file"/>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<input type=submit value="Submit">
<input type=reset value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
Controller Class(New Post)
// New Post
if(command.equals("/BoardAddAction.do")) {
try {
action = new BoardAddAction();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
// New Post form
}else if(command.equals("/BoardForm.do")) {
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_write.jsp");
BoardAddAction.java
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import dao.BoardDAO;
import model.BoardBean;
public class BoardAddAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardAddAction");
String path = request.getRealPath("boardupload");
System.out.println("path:"+path);
int size = 1024 * 1024; // 1MB
MultipartRequest multi =
new MultipartRequest(request,
path,
size,
"utf-8",
new DefaultFileRenamePolicy());
BoardBean board = new BoardBean();
board.setBoard_name(multi.getParameter("board_name"));
board.setBoard_pass(multi.getParameter("board_pass"));
board.setBoard_subject(multi.getParameter("board_subject"));
board.setBoard_content(multi.getParameter("board_content"));
board.setBoard_file(multi.getFilesystemName("board_file"));
BoardDAO dao = BoardDAO.getInstance();
int result = dao.insert(board);
if(result==1) {
System.out.println("insert");
}
ActionForward forward = new ActionForward();
forward.setRedirect(true);
forward.setPath("./BoardListAction.do");
return forward;
}
}
DAO Class (Main Post)
//Main Post(New post)
public int insert(BoardBean board) throws Exception {
int result=0;
SqlSession session = getSession();
result = session.insert("board_insert", board);
System.out.println("result:"+result);
return result;
}
Mapper Class(New Post)
<!-- New Post -->
<insert id="board_insert" parameterType="board">
insert into model22 values(model22_seq.nextval,#{board_name},
#{board_pass},#{board_subject},#{board_content},
#{board_file,jdbcType=VARCHAR},model22_seq.nextval,0,0,0,sysdate)
</insert>
qna_board_view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<table border=1 width=600 align=center>
<caption>Detail</caption>
<tr>
<td>Writer</td>
<td>${board.board_name}</td>
</tr>
<tr>
<td>View</td>
<td>${board.board_readcount}</td>
</tr>
<tr>
<td>Date</td>
<td>
<fmt:formatDate value="${board.board_date}"
pattern="MM/dd/yyyy H:mm"/>
</td>
</tr>
<tr>
<td>Title</td>
<td>${board.board_subject}</td>
</tr>
<tr>
<td>Content</td>
<td><pre>${board.board_content}</pre></td>
</tr>
<tr>
<td>Attached File</td>
<td>
<c:if test="${board.board_file != null }">
<a href="./board/file_down.jsp?file_name=${board.board_file}">${board.board_file}</a>
</c:if>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="button" value="Reply"
onClick="location.href='./BoardReplyAction.do?board_num=${board.board_num}&page=${page}&board_re_ref=${board.board_re_ref}&board_re_lev=${board.board_re_lev}&board_re_seq=${board.board_re_seq}'">
<input type="button" value="Edit"
onClick="location.href='./BoardModifyAction.do?board_num=${board.board_num}&page=${page}'">
<input type="button" value="Delete"
onClick="location.href='./BoardDeleteAction.do?board_num=${board.board_num}&page=${page}'">
<input type="button" value="List"
onClick="location.href='./BoardListAction.do?page=${page}'">
</td>
</tr>
</table>
Controller Class(Detail Page)
// Detail Page
}else if(command.equals("/BoardDetailAction.do")) {
try {
action = new BoardDetailAction();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
BoardDetailAction.java
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
import model.BoardBean;
public class BoardDetailAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardDetailAction");
int board_num = Integer.parseInt(request.getParameter("board_num"));
String page = request.getParameter("page");
BoardDAO dao = BoardDAO.getInstance();
// BoardBean board = dao.updateContent(board_num);
//Viewcount +1
dao.updateCount(board_num);
BoardBean board = dao.getContent(board_num);
request.setAttribute("board", board);
request.setAttribute("page", page);
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_view.jsp");
return forward;
}
}
DAO Class(Detail)
// Detail Page
public BoardBean getContent(int board_num) throws Exception {
BoardBean board = new BoardBean();
SqlSession session=getSession();
board = (BoardBean)session.selectOne("board_content", board_num);
return board;
}
Mapper Class(Detail)
<!-- Detail Page -->
<select id="board_content" parameterType="int" resultType="board">
select * from model22 where board_num = #{board_num}
</select>
qna_board_reply.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Bulletin Board</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath() %>/board/script.js"></script>
</head>
<body>
<form action="<%=request.getContextPath() %>/BoardReply.do" method="post">
<input type="hidden" name="board_num" value="${param.board_num}">
<input type="hidden" name="page" value="${param.page}">
<input type="hidden" name="board_re_ref" value="${param.board_re_ref}">
<input type="hidden" name="board_re_lev" value="${param.board_re_lev}">
<input type="hidden" name="board_re_seq" value="${param.board_re_seq}">
<table cellpadding="0" cellspacing="0" align=center border=1>
<tr align="center" valign="middle">
<td colspan="5">Reply</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Writer</div>
</td>
<td>
<input name="board_name" id="board_name" type="text" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Password</div>
</td>
<td>
<input name="board_pass" id="board_pass" type="password" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Title</div>
</td>
<td>
<input name="board_subject" id="board_subject" type="text" size="50" maxlength="100"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">Content</div>
</td>
<td>
<textarea name="board_content" id="board_content" cols="67" rows="15"></textarea>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<input type=submit value="Submit">
<input type=reset value="Canel">
</td>
</tr>
</table>
</form>
</body>
</html>
Controller Class(Reply)
// Reply form
}else if(command.equals("/BoardReplyAction.do")) {
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_reply.jsp");
// Reply
}else if(command.equals("/BoardReply.do")) {
try {
action = new BoardReply();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
BoardReply.java
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
import model.BoardBean;
public class BoardReply implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardReply");
request.setCharacterEncoding("utf-8");
int board_num = Integer.parseInt(request.getParameter("board_num"));
int board_re_ref = Integer.parseInt(request.getParameter("board_re_ref"));
int board_re_lev = Integer.parseInt(request.getParameter("board_re_lev"));
int board_re_seq = Integer.parseInt(request.getParameter("board_re_seq"));
String page = request.getParameter("page");
BoardBean board = new BoardBean();
board.setBoard_re_ref(board_re_ref);
board.setBoard_re_seq(board_re_seq);
BoardDAO dao = BoardDAO.getInstance();
dao.updateSeq(board);
board.setBoard_re_seq(board_re_seq+1);
board.setBoard_re_lev(board_re_lev+1);
board.setBoard_name(request.getParameter("board_name"));
board.setBoard_pass(request.getParameter("board_pass"));
board.setBoard_subject(request.getParameter("board_subject"));
board.setBoard_content(request.getParameter("board_content"));
dao.boardReply(board);
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/BoardDetailAction.do?board_num="+board_num+"&page="+page);
return forward;
}
}
DAO Class(Reply)
// Reply Print Order (board_re_seq value +1)
public void updateSeq(BoardBean board) {
SqlSession session = getSession();
session.update("board_updateseq", board);
}
// Write Reply
public void boardReply(BoardBean board) {
SqlSession session = getSession();
session.insert("board_reply", board);
}
Mapper Class(Reply)
<!-- Reply Print Order -->
<update id="board_updateseq" parameterType="board">
update model22 set board_re_seq=board_re_seq+1
where board_re_ref = #{board_re_ref} and board_re_seq > #{board_re_seq}
</update>
<!-- Write Reply -->
<insert id="board_reply" parameterType="board">
insert into model22 values(model22_seq.nextval,#{board_name},
#{board_pass},#{board_subject},#{board_content},
#{board_file,jdbcType=VARCHAR},#{board_re_ref},#{board_re_lev},#{board_re_seq},0,sysdate)
</insert>
qna_board_modify.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Edit</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath() %>/board/script.js"></script>
</head>
<body>
<form action="<%=request.getContextPath() %>/BoardModify.do" method="post">
<input type="hidden" name="board_num" value="${board.board_num}">
<input type="hidden" name="page" value="${page}">
<table cellpadding="0" cellspacing="0" align=center border=1>
<tr align="center" valign="middle">
<td colspan="5">Edit</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Writer</div>
</td>
<td>
<input name="board_name" id="board_name" type="text" size="10" maxlength="10"
value="${board.board_name }"/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Password</div>
</td>
<td>
<input name="board_pass" id="board_pass" type="password" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">제 목</div>
</td>
<td>
<input name="board_subject" id="board_subject" type="text" size="50" maxlength="100"
value="${board.board_subject}"/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">Content</div>
</td>
<td>
<textarea name="board_content" id="board_content" cols="67" rows="15">${board.board_content}</textarea>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<input type=submit value="Edit">
<input type=reset value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
Controller Class
// Edit form
}else if(command.equals("/BoardModifyAction.do")) {
try {
action = new BoardModifyAction();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
// Edit
}else if(command.equals("/BoardModify.do")) {
try {
action = new BoardModify();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
BoardModifyAction.java
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
import model.BoardBean;
public class BoardModifyAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardModifyAction");
int board_num = Integer.parseInt(request.getParameter("board_num"));
String page = request.getParameter("page");
BoardDAO dao = BoardDAO.getInstance();
BoardBean board = dao.getContent(board_num);
request.setAttribute("board", board);
request.setAttribute("page", page);
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_modify.jsp");
return forward;
}
}
BoardModify.java
package service;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
import model.BoardBean;
public class BoardModify implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardModify");
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String page = request.getParameter("page");
int board_num = Integer.parseInt(request.getParameter("board_num"));
String board_pass = request.getParameter("board_pass");
BoardBean board = new BoardBean();
board.setBoard_num(board_num);
board.setBoard_name(request.getParameter("board_name"));
board.setBoard_subject(request.getParameter("board_subject"));
board.setBoard_content(request.getParameter("board_content"));
BoardDAO dao = BoardDAO.getInstance();
BoardBean old = dao.getContent(board_num);
if(old.getBoard_pass().equals(board_pass)) {// Correct Password
dao.update(board);
}else { // Incorrect Password
out.println("<script>");
out.println("alert('Incorrect Password.');");
out.println("history.go(-1);");
out.println("</script>");
out.close();
return null;
}
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/BoardDetailAction.do?board_num="+board_num+"&page="+page);
return forward;
}
}
DAO Class(Edit)
// Edit
public void update(BoardBean board) throws Exception {
SqlSession session=getSession();
session.update("board_update", board);
}
Mapper Class(Edit)
<!-- Edit -->
<update id="board_update" parameterType="board">
update model22 set board_name=#{board_name}, board_subject=#{board_subject},
board_content=#{board_content} where board_num=#{board_num}
</update>
qna_board_delete.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Delete</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath() %>/board/script.js"></script>
</head>
<body>
<form action="<%=request.getContextPath() %>/BoardDelete.do" method="post">
<input type="hidden" name="board_num" value="${param.board_num}">
<input type="hidden" name="page" value="${param.page}">
<table width=300 cellpadding="0" cellspacing="0" align=center border=1>
<tr align="center" valign="middle">
<td colspan="5">Delete</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">Password</div>
</td>
<td>
<input name="board_pass" id="board_pass" type="password" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<input type=submit value="Delete">
<input type=reset value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
Controller Class(Delete)
// Delete form
}else if(command.equals("/BoardDeleteAction.do")) {
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/board/qna_board_delete.jsp");
// Delete
}else if(command.equals("/BoardDelete.do")) {
try {
action = new BoardDelete();
forward = action.execute(request, response);
}catch(Exception e) {
e.printStackTrace();
}
BoardDeleteAction.java
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BoardDeleteAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardDeleteAction");
ActionForward forward = new ActionForward();
return forward;
}
}
BoardDelete.java
package service;
import java.io.File;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDAO;
import model.BoardBean;
public class BoardDelete implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("BoardDelete");
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String path = request.getRealPath("boardupload");
int board_num = Integer.parseInt(request.getParameter("board_num"));
String page = request.getParameter("page");
String board_pass = request.getParameter("board_pass");
BoardDAO dao = BoardDAO.getInstance();
BoardBean old = dao.getContent(board_num);
if(old.getBoard_pass().equals(board_pass)) { //Correct Password
dao.delete(board_num);
if(old.getBoard_file() != null) {
File file = new File(path);
file.mkdirs();
File[] f = file.listFiles();
for(int i=0; i<f.length; i++) {
if(f[i].getName().equals(old.getBoard_file())) {
f[i].delete();
}
}
}
}else { // Incorrect Password
out.println("<script>");
out.println("alert('Incorrect Password.');");
out.println("history.go(-1);");
out.println("</script>");
out.close();
return null;
}
ActionForward forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/BoardListAction.do?page="+page);
return forward;
}
}
DAO Class(Delete)
// Delete
public void delete(int board_num) {
SqlSession session=getSession();
session.delete("board_delete", board_num);
}
Mapper Class(Delete)
<!-- Delete -->
<delete id="board_delete" parameterType="int">
delete from model22 where board_num=#{board_num}
</delete>
Sequence : View -> Controller class -> Service class -> DAO -> Mapper class