Configuration
1. pom.xml - libraries/dependencies
2. web.xml
3. servlet-context.xml - prefix/suffix
4. mybatis-config.xml - MyBatis configuration file (Resources directory)
5. board.xml - mapper file (Resources directory), SQL
6. root-context.xml - beans
Database
board1.sql
select * from tab;
select * from board;
drop table board purge;
create table board (
num number primary key,
writer varchar2(20) not null,
subject varchar2(50) not null, -- Title
topic varchar2(50) not null, -- Topic
content varchar2(500) not null,
email varchar2(30) ,
readcount number default 0,
passwd varchar2(12) not null,
ref number not null,
re_step number not null,
re_level number not null,
ip varchar2(20) not null,
reg_date date not null,
del char(1)
);
update board set readcount = 51 where num = 250;
In this project, service class and dao class will be divided into two files: interface and implementation class.
BoardService.java
package board1.service;
import java.util.List;
import board1.model.Board;
public interface BoardService {
// List<Board> list(int startRow, int endRow);
List<Board> list(Board board);
int getTotal(Board board);
int insert(Board board);
Board select(int num);
void selectUpdate(int num);
int update(Board board);
int delete(int num);
int getMaxNum();
void updateRe(Board board);
}
BoardDAO.java
package board1.dao;
import java.util.List;
import board1.model.Board;
public interface BoardDao {
// List<Board> list(int startRow, int endRow);
List<Board> list(Board board);
int getTotal(Board board);
int insert(Board board);
Board select(int num);
void selectUpdate(int num);
int update(Board board);
int delete(int num);
int getMaxNum();
void updateRe(Board board);
}
index.jsp
insertForm.jsp
BoardController.java(New post and Reply)
@RequestMapping("insertForm.do") // InsertForm
public String insertForm(String nm, String pageNum, Model model) {
int num = 0, ref = 0, re_level = 0, re_step = 0;
if (nm != null) { // Reply
num = Integer.parseInt(nm);
Board board = bs.select(num);
ref = board.getRef();
re_level = board.getRe_level();
re_step = board.getRe_step();
}
model.addAttribute("num", num);
model.addAttribute("ref", ref);
model.addAttribute("re_level", re_level);
model.addAttribute("re_step", re_step);
model.addAttribute("pageNum", pageNum);
return "insertForm";
}
@RequestMapping("insert.do") // Insert
public String insert(Board board, Model model, HttpServletRequest request) {
int num = board.getNum();
int number = bs.getMaxNum();
if (num != 0) { // Reply
bs.updateRe(board);
board.setRe_level(board.getRe_level() + 1);
board.setRe_step(board.getRe_step() + 1);
} else // Original post
board.setRef(number);
board.setNum(number);
String ip = request.getRemoteAddr();
board.setIp(ip);
int result = bs.insert(board);
model.addAttribute("result", result);
return "insert";
}
BoardServiceImpl.java
public int insert(Board board) {
return bd.insert(board);
}
BoardDaoImpl.java
public int insert(Board board) {
return sst.insert("boardns.insert",board);
}
Board.xml
<insert id="insert" parameterType="board">
<!--<selectKey keyProperty="num"
order="BEFORE" resultType="int">
select nvl(max(num),0) + 1 from board
</selectKey> -->
insert into board values (#{num},#{writer},#{subject},#{topic},
#{content},#{email},0,#{passwd},#{ref},
#{re_step},#{re_level},#{ip},sysdate,'n')
</insert>
<!-- Reply -->
<update id="update" parameterType="board">
update board set writer=#{writer},topic=#{topic},subject=#{subject},
content=#{content},email=#{email} where num=#{num}
</update>
insert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("Successfully posted.");
location.href = "list.do";
</script>
</c:if>
<c:if test="${result <= 0 }">
<script type="text/javascript">
alert("Failed to post.");
history.go(-1);
</script>
</c:if>
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="container" align="center">
<h2 class="text-primary">Better Call Saul/ Breaking Bad/ El
Camino universe Forum</h2>
<table class="table table-striped">
<tr>
<td>No.</td>
<td>Subject</td>
<td>Title</td>
<td>Writer</td>
<td>Date</td>
<td>View</td>
</tr>
<c:if test="${empty list}">
<tr>
<td colspan="5">No posts yet. Be the first writer!</td>
</tr>
</c:if>
<c:if test="${not empty list}">
<c:set var="no1" value="${no }"></c:set>
<c:forEach var="board" items="${list }">
<tr>
<td>${no1}</td>
<c:if test="${board.del =='y' }">
<td colspan="4">This post is deleted.</td>
</c:if>
<c:if test="${board.del !='y' }">
<td> <c:if test="${board.re_level >0 }">
<img alt="" src="images/level.gif" height="2"
width="${board.re_level *5 }">
<img alt="" src="images/re.gif">
</c:if> ${board.topic}
</a></td>
<td><a href="view.do?num=${board.num}&pageNum=${pp.currentPage}"
class="btn btn-default"><c:if test="${board.readcount > 10 }">
<img alt="" src="images/hot.gif">
</c:if>${board.subject}</td>
<td>${board.writer}</td>
<td>${board.reg_date}</td>
<td>${board.readcount}</td>
</c:if>
</tr>
<c:set var="no1" value="${no1 - 1}" />
</c:forEach>
</c:if>
</table>
<form action="list.do">
<input type="hidden" name="pageNum" value="1"> <select
name="search">
<option value="subject"
<c:if test="${search=='subject'}">selected="selected" </c:if>>Title</option>
<option value="content"
<c:if test="${search=='content'}">selected="selected" </c:if>>Content</option>
<option value="writer"
<c:if test="${search=='writer'}">selected="selected" </c:if>>Writer</option>
<option value="subcon"
<c:if test="${search=='subcon'}">selected="selected" </c:if>>Title+Content</option>
</select> <input type="text" name="keyword">
<input type="submit" value="Submit">
</form>
<ul class="pagination">
<!-- Pagination : Searched -->
<c:if test="${not empty keyword}">
<c:if test="${pp.startPage > pp.pagePerBlk }">
<li><a
href="list.do?pageNum=${pp.startPage - 1}&search=${search}&keyword=${keyword}">Prev</a></li>
</c:if>
<c:forEach var="i" begin="${pp.startPage}" end="${pp.endPage}">
<li <c:if test="${pp.currentPage==i}">class="active"</c:if>><a
href="list.do?pageNum=${i}&search=${search}&keyword=${keyword}">${i}</a></li>
</c:forEach>
<c:if test="${pp.endPage < pp.totalPage}">
<li><a
href="list.do?pageNum=${pp.endPage + 1}&search=${search}&keyword=${keyword}">Next</a></li>
</c:if>
</c:if>
<!-- Pagination : List -->
<c:if test="${empty keyword}">
<c:if test="${pp.startPage > pp.pagePerBlk }">
<li><a href="list.do?pageNum=${pp.startPage - 1}">Next</a></li>
</c:if>
<c:forEach var="i" begin="${pp.startPage}" end="${pp.endPage}">
<li <c:if test="${pp.currentPage==i}">class="active"</c:if>><a
href="list.do?pageNum=${i}">${i}</a></li>
</c:forEach>
<c:if test="${pp.endPage < pp.totalPage}">
<li><a href="list.do?pageNum=${pp.endPage + 1}">Prev</a></li>
</c:if>
</c:if>
</ul>
<div align="center">
<a href="insertForm.do" class="btn btn-info">New Post</a>
</div>
</div>
</body>
</html>
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
$(function() {
$('#list').load('list.do?pageNum=${pageNum}');
});
</script>
</head>
<body>
<div class="container" align="center">
<h2 class="text-primary">Post</h2>
<table class="table table-bordered">
<tr>
<td>Subject</td>
<td>${board.topic}</td>
</tr>
<tr>
<td>Title</td>
<td>${board.subject}</td>
</tr>
<tr>
<td>Writer</td>
<td>${board.writer}</td>
</tr>
<tr>
<td>View</td>
<td>${board.readcount}</td>
</tr>
<tr>
<td>IP Address</td>
<td>${board.ip}</td>
</tr>
<tr>
<td>Email</td>
<td>${board.email}</td>
</tr>
<tr>
<td>Content</td>
<td><pre>${board.content}</pre></td>
</tr>
</table>
<a href="list.do?pageNum=${pageNum}" class="btn btn-info">List</a>
<a href="updateForm.do?num=${board.num}&pageNum=${pageNum}"
class="btn btn-info">Edit</a>
<a href="deleteForm.do?num=${board.num}&pageNum=${pageNum}"
class="btn btn-info">Delete</a>
<a href="insertForm.do?nm=${board.num}&pageNum=${pageNum}"
class="btn btn-info">Reply</a>
<div id="list"></div>
</div>
</body>
</html>
BoardController.java
@RequestMapping("view.do")
public String view(int num, String pageNum, Model model) {
bs.selectUpdate(num); // view +1
Board board = bs.select(num);
model.addAttribute("board", board);
model.addAttribute("pageNum", pageNum);
return "view";
}
updateForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function chk() {
if(frm.passwd.value != frm.passwd2.value) {
alert("Incorrect password.");
frm.passwd2.focus();
frm.passwd2.value =""; // To erase the inserted password.
return false;
}
}
</script>
</head>
<body>
<div class="container" align="center">
<h2 class="text-primary">Edit post</h2>
<form action="update.do" method="post" name="frm"
onsubmit="return chk()">
<input type="hidden" name="num" value="${board.num}">
<input type="hidden" name="pageNum" value="${pageNum}">
<input type="hidden" name="passwd" value="${board.passwd}">
<table class="table table-striped">
<tr>
<td>No.</td>
<td>${board.num}</td>
</tr>
<tr>
<td>Subject
<select name="topic" type="text">
<option value="Breaking Bad" selected="selected">Breaking Bad</option>
<option value="Better Call Saul" selected="selected">Better Call Saul</option>
<option value="El Camino" selected="selected">El Camino</option>
</select>
</td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="subject" required="required"
value="${board.subject}"></td>
</tr>
<tr>
<td>Writer</td>
<td><input type="text" name="writer" required="required"
value="${board.writer}"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" required="required"
value="${board.email}"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="passwd2" required="required"></td>
</tr>
<tr>
<td>Content</td>
<td>
<textarea rows="5" cols="30" name="content" required="required">${board.content}
</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
update.jsp
BoardController.java
@RequestMapping("updateForm.do") // UpdateForm
public String updateForm(int num, String pageNum, Model model) {
Board board = bs.select(num);
model.addAttribute("board", board);
model.addAttribute("pageNum", pageNum);
return "updateForm";
}
@RequestMapping("update.do") // Update
public String update(Board board, String pageNum, Model model) {
int result = bs.update(board);
model.addAttribute("result", result);
model.addAttribute("pageNum", pageNum);
return "update";
}
BoardService.java
public int update(Board board) {
return bd.update(board);
}
BoardDao.java
public int update(Board board) {
return sst.update("boardns.update",board);
}
board.xml
<update id="update" parameterType="board">
update board set writer=#{writer},topic=#{topic},subject=#{subject},
content=#{content},email=#{email} where num=#{num}
</update>
deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function chk() {
if (frm.passwd.value != frm.passwd2.value) {
alert("Incorrect Password");
frm.passwd2.focus();
frm.passwd2.value =""; // To erase the inserted password.
return false;
}
}
</script>
</head>
<body>
<div class="container">
<h2 class="text-primary">Delete Post</h2>
<form action="delete.do" name="frm" onsubmit="return chk()" method="post">
<input type="hidden" name="pageNum" value="${pageNum}">
<input type="hidden" name="passwd" value="${board.passwd}">
<input type="hidden" name="num" value="${board.num}">
<table class="table">
<tr>
<td>Password</td>
<td><input type="password" name="passwd2" required="required"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("Successfully deleted.");
location.href = "list.do?pageNum=${pageNum}";
</script>
</c:if>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("Failed to delete.");
history.go(-1);
</script>
</c:if>
</body>
</html>
BoardController.java
@RequestMapping("deleteForm.do")
public String deleteForm(int num, String pageNum, Model model) {
Board board = bs.select(num);
model.addAttribute("board", board);
model.addAttribute("pageNum", pageNum);
return "deleteForm";
}
@RequestMapping("delete.do")
public String delete(int num, String pageNum, Model model) {
int result = bs.delete(num);
model.addAttribute("result", result);
model.addAttribute("pageNum", pageNum);
return "delete";
}
BoardService.java
public int delete(int num) {
return bd.delete(num);
}
BoardDao.java
public int delete(int num) {
return sst.update("boardns.delete",num);
}