[Day6] JSP/Servlet 6 [12/9]
[ JSP + Servlet = ๊ฒ์ํ(board) ]
๋ก์ง์ฒ๋ฆฌ๋ด๋น
ํ๋ฉด์ถ๋ ฅ๋ด๋น
days05 > board ํด๋ ์์ฑ
1. JDBC ๊ฒ์ํ
http://taeyo.net/ ๊ฒ์ํ(board) ํ ์ด๋ธ ์์ฑ
CREATE SEQUENCE seq_tbl_cstVSBoard;
CREATE TABLE tbl_cstVSBoard(
seq NUMBER NOT NULL PRIMARY KEY
, writer VARCHAR2(20) NOT NULL
, pwd VARCHAR2(20) NOT NULL
, email VARCHAR2(100)
, title VARCHAR2(200) NOT NULL
, writedate DATE DEFAULT SYSDATE
, readed NUMBER DEFAULT 0
, tag NUMBER(1) DEFAULT 0 -- 0 ํ ์คํธ๋ชจ๋ 1 HTML๋ชจ๋
, content CLOB
);
2. sql developer ์คํ -> ์ํ์ค ํ์ธ, ํ ์ด๋ธ ํ์ธ.
TBL_CSTVSBOARD
SEQ_TBL_CSTVSBOARD
4. BoardDTO.java
BoardDAO.java
BoardDAOImpl.java ๋ณต์ฌ + ๋ถ์ด๊ธฐ
5. a -> Write.java -> write.jsp
์๋ธ๋ฆฟ
404 ์๋ฌ 404์๋ฌ
/jspPro/cstvsboard/wirte.htm
์๋ธ๋ฆฟ url-pattern ํ์ธ
<a href="<%= contextPath %>/cstvsboard/wirte.htm">๊ฒ์ํ ๊ธ ์ฐ๊ธฐ(Get ๋ฐฉ์ ์์ฒญ)</a><br>
* ๋งํฌํ๊ทธ๋๊น GET๋ฐฉ์
<%@ include file="/WEB-INF/inc/include.jspf"%>
* ์ฌ๊ธฐ ์ ์ธ๋ ๋ณ์๋ค ์ฌ์ฉ๊ฐ๋ฅ (contextpath)
Write.java ์๋ธ๋ฆฟ ๋ง๋ค๊ธฐ
package days05.board;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
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.util.DBConn;
@WebServlet("/cstvsboard/wirte.htm")
public class Write extends HttpServlet {
private static final long serialVersionUID = 1L;
public Write() {
super();
}
// /jspPro/cstvsboard/wirte.htm(get)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> Write.doGet() ํธ์ถ๋จ...");
// (์ถ๊ฐ) ๋ก๊ทธ์ธ O ๋๋ ๊ถํ O ์ฒดํฌ -> ๋ก๊ทธ์ธํ์ด์ง ์ด๋.
// ํฌ์๋ฉ
// http://localhost/jspPro/cstvsboard/write.htm
// String path = "board/write.jsp";
// http://localhost/jspPro/cstvsboard/board/write.jsp
String path = "/days05/board/write.jsp";
//http://localhost/jspPro/days05/board/write.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
// /jspPro/cstvsboard/wirte.htm(post)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> Write.doPost() ํธ์ถ๋จ...");
// ํฐ์บฃ 8.X post ์์ฒญ ISO 8859-1 ํ๊ธX ( ๊ต์ฌ ์ธ์ฝ๋ฉ ์ค๋ช
)
request.setCharacterEncoding("UTF-8");
// ์
๋ ฅ๋ฐ์ ์ ๊ฒ์๊ธ ์ ๋ณด๋ฅผ ์ป์ด์์
// DB insert~
String writer = request.getParameter("writer") ;
String pwd = request.getParameter("pwd") ;
String email = request.getParameter("email") ;
String title = request.getParameter("title") ;
String content = request.getParameter("content") ;
int tag = Integer.parseInt( request.getParameter("tag") );
BoardDTO dto = new BoardDTO(0, writer, pwd, email, title, null, 0, tag, content);
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
rowCount = dao.insert(dto);
} catch (SQLException e) {
System.out.println("> Write.doPost() Exception...");
e.printStackTrace();
}
DBConn.close();
// ์ ? [list.htm ๋ฆฌ๋ค์ด๋ ํธ O] / ํฌ์๋ฉ X
String location = "/jspPro/cstvsboard/list.htm";
if( rowCount == 1) {
location +="?write=success";
}else {
location +="?write=fail";
}
response.sendRedirect(location);
}
}
* ๋ชฉ๋ก ๋ณด์ฌ์ค๋ list htm ๋์์ผํ๊ธฐ ๋๋ฌธ์ ๋ฆฌ๋ค์ด๋ ํธ (POST)
-write.jsp-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 3:28:25</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
</style>
</head>
<body>
<h3>write.jsp</h3>
<div align="center">
<h2>๊ธ ์ฐ๊ธฐ</h2>
<!--
action ์์ฑ ์๋ต : http://localhost/jspPro/cstvsboard/write.htm
-> Write.java ์๋ธ๋ฆฟ
-> doPost()
-->
<form method="post">
<table style="padding: 2px; width: 600px">
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ ์ด์ฃผ์ธ์</b></td>
</tr>
<tr>
<td align="center">์ด๋ฆ</td>
<td><input type="text" name="writer" size="15"
autofocus="autofocus" required="required"></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td><input type="password" name="pwd" size="15" required="required"></td>
</tr>
<tr>
<td align="center">Email</td>
<td><input type="email" name="email" size="50" ></td>
</tr>
<tr>
<td align="center">์ ๋ชฉ</td>
<td><input type="text" name="title" size="50" required="required"></td>
</tr>
<tr>
<td align="center">๋ด์ฉ</td>
<td><textarea name="content" cols="50" rows="10"></textarea></td>
</tr>
<tr>
<td align="center">HTML</td>
<td><input type="radio" name="tag" value="1" checked>์ ์ฉ
<input type="radio" name="tag" value="0">๋น์ ์ฉ</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์์ฑ ์๋ฃ">
<input type="reset" value="๋ค์ ์์ฑ">
<a href="<%= contextPath %>/cstvsboard/list.htm">Home</a>
</td>
</tr>
</table>
</form>
</div>
<script>
$(function (){
$("form").submit(function (){
alert("์ ํจ์ฑ ๊ฒ์ฌ ์ฒดํฌ ํ๋ค. ");
// event.preventDefault();
// return false;
});
});
</script>
-list.java-
package days05.board;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
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.util.DBConn;
@WebServlet("/cstvsboard/list.htm")
public class List extends HttpServlet {
private static final long serialVersionUID = 1L;
public List() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("> Write.doGet() get+post ํธ์ถ๋จ...");
int currentPage = 1 ; // ํ์ฌ ํ์ด์ง.
int numberPerPage = 10; // ํ ํ์ด์ง์ ์ถ๋ ฅํ ๊ฒ์๊ธ ์
int numberOfPageBlock = 10; // ํ์ด์ง ๋ธ๋ญ ์ [1] 2 3 4 5 6 7 8 9 10 > >>
ArrayList<BoardDTO> list = null;
// ํ์ด์ง ์ฒ๋ฆฌ
int totalRecords, totalPages = 0;
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
try {
list = dao.select(currentPage, numberPerPage);
// ํ์ด์ง ์ฒ๋ฆฌ
} catch (SQLException e) {
System.out.println("> List.doGet() Exception...");
e.printStackTrace();
}
DBConn.close();
request.setAttribute("list", list);
// ํฌ์๋ฉ
String path = "/days05/board/list.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
-list.jsp-
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 4:45:18</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
tbody tr td:nth-of-type(2) {
text-align: left;
}
a {
text-decoration: none;
color: black;
}
a:hover {
color: red;
}
</style>
<!-- ํ์ด์ง ์ฒ๋ฆฌ style -->
<style>
.pagination {
margin: 0 auto;
display: flex;
justify-content: center;
}
.pagination a {
color: black;
float: left;
padding: 4px 8px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a.active {
background-color: dodgerblue;
color: white;
}
.pagination a:hover:not (.active ) {
background-color: #ddd;
}
</style>
<style>
.searchWord {
background-color: yellow;
color: red;
}
</style>
<style>
.title {
display: inline-block;
white-space: nowrap;
width: 90%;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<h3>list.jsp</h3>
<div align="center">
<h2>๋ชฉ๋ก ๋ณด๊ธฐ</h2>
<a href="<%= contextPath %>/cstvsboard/write.htm">๊ธ์ฐ๊ธฐ</a>
<table style="width:600px;">
<thead>
<tr>
<th width="10%">๋ฒํธ</th>
<th width="45%">์ ๋ชฉ</th>
<th width="17%">์์ฑ์</th>
<th width="20%">๋ฑ๋ก์ผ</th>
<th width="10%">์กฐํ</th>
</tr>
</thead>
<tbody>
<c:choose>
<c:when test="${ not empty list }">
<c:forEach items="${ list }" var="dto">
<tr>
<td>${ dto.seq }</td>
<td><a href="<%= contextPath %>/cstvsboard/view.htm?seq=${ dto.seq }">${dto.title }</a></td>
<td><a href="mailto:${ dto.email }">${ dto.writer }</a></td>
<td>${ dto.writedate }</td>
<td>${ dto.readed }</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="5">๋ฑ๋ก๋ ๊ฒ์๊ธ์ด ์์ต๋๋ค.</td>
</tr>
</c:otherwise>
</c:choose>
</tbody>
<tfoot>
<tr>
<td colspan="5" align="center">
<div class="pagination">
<a href="#" class="active"> 1 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=2"> 2 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=3"> 3 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=4"> 4 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=5"> 5 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=6"> 6 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=7"> 7 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=8"> 8 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=9"> 9 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=10"> 10 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=11"> » </a>
</div>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<form method="get">
<select name="searchCondition" id="searchCondition">
<option value="1">title</option>
<option value="2">content</option>
<option value="3">writer</option>
<option value="4">title+content</option>
</select>
<input type="text" name="searchWord" id="searchWord" />
<input type="submit" value="search" />
</form>
</td>
</tr>
</tfoot>
</table>
</div>
๊ธ ๋ด์ฉ ์์ฒญ
๊ฒ์๊ธ ๋ชฉ๋ก์์ ํด๋น ๊ฒ์๊ธ ํด๋ฆญํ๋ฉด ์ด๋ํ๊ฒ aํ๊ทธ ๊ฑธ์ด์ฃผ๊ณ -> ์ฟผ๋ฆฌ๋ก seq ๋ฌ์ ๋ค ๋ค๋ฅธ ํ๊ทธ๋ก ๋ง๋ค์ด์ค (get, ์ฟผ๋ฆฌ๋ก )
-> ์๋ธ๋ฆฟ View.java -> doGet() {
1) ๋ด์ฉ ๊ธ ๋ฒํธ ํ๋ผ๋ฏธํฐ ?seq = 381
2) DB ๊ฒ์๊ธ ์ ๋ณด ์ป์ด์ด -> ์ ์ฅํด์ ์ด๋์ํค๊ธฐ ๋๋ฌธ์ ํฌ์๋ฉ
3) ํฌ์๋ฉ
list.jsp
<c:choose>
<c:when test="${ not empty list }">
<c:forEach items="${ list }" var="dto">
<tr>
<td>${ dto.seq }</td>
<td><a href="<%= contextPath %>/cstvsboard/view.htm?seq=${ dto.seq }">${dto.title }</a></td>
<td><a href="mailto:${ dto.email }">${ dto.writer }</a></td>
<td>${ dto.writedate }</td>
<td>${ dto.readed }</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="5">๋ฑ๋ก๋ ๊ฒ์๊ธ์ด ์์ต๋๋ค.</td>
</tr>
</c:otherwise>
</c:choose>
View.java
package days05.board;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
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.util.DBConn;
@WebServlet("/cstvsboard/view.htm")
public class View extends HttpServlet {
private static final long serialVersionUID = 1L;
public View() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ?seq=381
// System.out.println("> View.doGet() ํธ์ถ๋จ...");
// 1.
int seq = Integer.parseInt(request.getParameter("seq"));
// 2. ํด๋น ๊ฒ์๊ธ์ ์กฐํ์ 1์ฆ๊ฐ + ํด๋น ๊ฒ์๊ธ ์ ๋ณด๋ฅผ ์ป์ด์ค๋ ์์
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
BoardDTO dto = null;
try {
dao.increaseReaded(seq);
dto = dao.view(seq);
} catch (SQLException e) {
System.out.println("> View.doGet() Exception...");
e.printStackTrace();
}
request.setAttribute("dto", dto);
// 3. ํฌ์๋ฉ
String path = "/days05/board/view.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- ์์ฟผ์ค ๋ฐ์์์ ๊ฒ์๊ธ ์กฐํ์ +1 & ๊ฒ์๊ธ ์ ๋ณด ์ป์ด์ค๊ธฐ
view.jsp
<style>
table{
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
border:solid 1px gray;
/* border-radius: 3px;
padding:3px; */
}
#tblContent{
width:600px;
}
</style>
</head>
<body>
<h3>view.jsp</h3>
<!-- 11:22 ์์
์์ -->
<div align="center">
<h2>๋ด์ฉ๋ณด๊ธฐ</h2>
<!-- <table id="tblContent" class="table"> -->
<table id="tblContent">
<tr>
<td>์ด๋ฆ</td>
<td>${ dto.writer }</td>
<td>๋ฑ๋ก์ผ</td>
<td>${ dto.writedate }</td>
</tr>
<tr>
<td>Email</td>
<td><a href="mailto:${ dto.email }">${ dto.email }</a></td>
<td>์กฐํ</td>
<td>${ dto.readed }</td>
</tr>
<tr>
<td>์ ๋ชฉ</td>
<td colspan="3">${ dto.title }</td>
</tr>
<tr>
<td colspan="4" style="padding:15px;height: 200px;text-align: left;vertical-align: top">
${ dto.content }
</td>
</tr>
<tr>
<td colspan="4" align="center">
<a class="btn btn-secondary" href="<%= contextPath %>/cstvsboard/edit.htm?seq=${ dto.seq }" id="editLink">์์ ํ๊ธฐ</a>
<a class="btn btn-secondary" href="<%= contextPath %>/cstvsboard/delete.htm?seq=${ dto.seq }" id="deleteLink">์ญ์ ํ๊ธฐ</a>
<a class="btn btn-secondary" href="<%= contextPath %>/cstvsboard/list.htm" id="homeLink">HOme</a>
<input class="btn btn-secondary" type="button" id="btnModalDelete" value="๋ชจ๋ฌ์ฐฝ์ผ๋ก ๊ธ์ญ์ ">
</td>
</tr>
</table>
</div>
<!-- ์ญ์ ๋ชจ๋ฌ์ฐฝ div ํ๊ทธ -->
<div id="dialog-form" align="center" title="์ญ์ ">
<h2>์ญ์ ํ๊ธฐ</h2>
<form method="post" action="<%= contextPath%>/cstvsboard/delete.htm?seq=${ param.seq}">
<table>
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ญ์ ํฉ๋๋ค</b></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td>
<input type="password" name="pwd" size="15" autofocus="autofocus">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์ญ์ ">
<input type="button" id="cancel" value="์ทจ์">
</td>
</tr>
</table>
</form>
</div>
<script>
// 1.
dialog = $("#dialog-form").dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
buttons:{},
close: function (){
form[0].reset();
}
});
//2.
form = dialog.find("form");
// 3. ๋ชจ๋ฌ์ฐฝ ์ด๊ธฐ
$("#btnModalDelete").on("click", function(event) {
dialog.dialog("open" );
})
// 4. ๋ชจ๋ฌ์ฐฝ ๋ซ๊ธฐ
$("#cancel").on("click", function(event) {
dialog.dialog("close" );
})
</script>
<script>
// view.jsp
// edit.htm?seq=350&edit=success
if( '<%= request.getParameter("edit") %>' == 'success' ){
alert("๊ธ ์์ ์๋ฃ!!!")
}else if( '<%= request.getParameter("edit") %>' == 'fail' ){
alert("๊ธ ์์ ์คํจ( ๋น๋ฐ๋ฒํธ ์๋ชป)!!!")
}
</script>
ํ์ด์ง ์ฒ๋ฆฌ
[1] 2 3 4 5 6 7 8 9 10
์์๋ฒํธ ๋๋ฒํธ
ํ์ด์ง๋ธ๋ญ์(10)
์ด์ ๋ฒํผ
๋ค์๋ฒํผ
์์์ผํ๋ ๊ฒ : ์ด๋ ์ฝ๋์, ์ดํ์ด์ง์, ํ์ด์ง ๋ธ๋ญ์
Pageblock
package days05.board;
public class PageBlock {
// ํ๋
private int currentPage; // ํ์ฌํ์ด์ง
private int numberPerPage = 10; // ํ ํ์ด์ง ์ถ๋ ฅ ๊ฒ์๊ธ ์
private int numberOfPageBlock = 10; // ํ์ด์ง ๋ธ๋ญ ์
private int startOfPageBlock = 1; // ์์ ๋ฒํธ 1 2 [3] 4 5 6 7 8 9 10 >
private int endOfPageBlock ; // ๋ ๋ฒํธ
private boolean prev, next; // ์ด์ ๋ฒํธ, ๋ค์ ๋ฒํผ
// ์์ฑ์
public PageBlock() {
super();
}
public PageBlock(int currentPage, int numberPerPage, int numberOfPageBlock, int startOfPageBlock,
int endOfPageBlock, boolean prev, boolean next) {
super();
this.currentPage = currentPage;
this.numberPerPage = numberPerPage;
this.numberOfPageBlock = numberOfPageBlock;
this.startOfPageBlock = startOfPageBlock;
this.endOfPageBlock = endOfPageBlock;
this.prev = prev;
this.next = next;
}
// getter, setter
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getNumberPerPage() {
return numberPerPage;
}
public void setNumberPerPage(int numberPerPage) {
this.numberPerPage = numberPerPage;
}
public int getNumberOfPageBlock() {
return numberOfPageBlock;
}
public void setNumberOfPageBlock(int numberOfPageBlock) {
this.numberOfPageBlock = numberOfPageBlock;
}
public int getStartOfPageBlock() {
return startOfPageBlock;
}
public void setStartOfPageBlock(int startOfPageBlock) {
this.startOfPageBlock = startOfPageBlock;
}
public int getEndOfPageBlock() {
return endOfPageBlock;
}
public void setEndOfPageBlock(int endOfPageBlock) {
this.endOfPageBlock = endOfPageBlock;
}
public boolean isPrev() {
return prev;
}
public void setPrev(boolean prev) {
this.prev = prev;
}
public boolean isNext() {
return next;
}
public void setNext(boolean next) {
this.next = next;
}
}
- ์์
1) PageBlock ํด๋์ค ์ ์ธ
2) List.java ์๋ธ๋ฆฟ
PageBlock
request.setAttribute("",)
int currentPage = 1 ; // ํ์ฌ ํ์ด์ง.
try { currentPage=Integer.parseInt(request.getParameter("currentpage")) ;}catch(Exception e) {}
int numberPerPage = 10; // ํ ํ์ด์ง์ ์ถ๋ ฅํ ๊ฒ์๊ธ ์
int numberOfPageBlock = 10; // ํ์ด์ง ๋ธ๋ญ ์ [1] 2 3 4 5 6 7 8 9 10 > >>
// ํ์ด์ง ์ฒ๋ฆฌ
pageBlock = pagingService(currentPage, numberPerPage, numberOfPageBlock , dao
, searchCondition, searchWord );
// ๋ณต์ฌ ๋ถ์ด๊ธฐ JDBC BoardService. pagingService()
public PageBlock pagingService(int currentPage, int numberPerPage, int numberOfPageBlock, BoardDAOImpl dao
, int searchCondition, String searchWord ) {
PageBlock pageBlock = null;
int totalRecords =0;
int totalPages = 1;
try {
// [๊ฒ์ ํ์ด์ง ์ฒ๋ฆฌ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 3 S
if( searchWord == "" ) {
totalRecords = dao.getTotalRecords() ; // ์ ์ฒด ๋ ์ฝ๋ ์
totalPages = dao.getTotalPages( numberPerPage ) ; // ์ ์ฒด ํ์ด์ง ์
}else {
totalRecords = dao.getTotalRecords(searchCondition, searchWord); // ๊ฒ์๋ ๊ฒฐ๊ณผ์ ๋ ์ฝ๋ ์
totalPages = dao.getTotalPages( numberPerPage,searchCondition, searchWord ) ; // ๊ฒ์๋ ํ์ด์ง ์
}
// [๊ฒ์ ํ์ด์ง ์ฒ๋ฆฌ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 3 E
int begin = ( currentPage - 1)/numberOfPageBlock * numberOfPageBlock + 1;
int end = begin + numberOfPageBlock - 1;
if( end > totalPages ) end = totalPages;
boolean prev = begin == 1 ? false : true;
boolean next = end == totalPages ? false: true;
pageBlock = new PageBlock(currentPage, numberPerPage, numberOfPageBlock ,begin, end, prev, next);
} catch( SQLException e) {
e.printStackTrace();
}
return pageBlock;
}
} // class
3) list.jsp
<td colspan="5" align="center">
<div class="pagination">
<%--
<a href="#" class="active"> 1 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=2"> 2 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=3"> 3 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=4"> 4 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=5"> 5 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=6"> 6 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=7"> 7 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=8"> 8 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=9"> 9 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=10"> 10 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=11"> » </a>
--%>
<c:if test="${ pageBlock.prev }">
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ pageBlock.startOfPageBlock - 1 }"> « </a>
</c:if>
<c:forEach begin="${ pageBlock.startOfPageBlock }" end="${ pageBlock.endOfPageBlock }" var="i" step="1">
<c:choose>
<c:when test="${ pageBlock.currentPage eq i }">
<a href="#" class="active">${ i }</a>
</c:when>
<c:otherwise>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ i }">${ i }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${ pageBlock.next }">
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ pageBlock.endOfPageBlock + 1 }"> » </a>
</c:if>
๊ฒ์์ฒ๋ฆฌ
list.htm?searchCondition=2&searchWord=~
-> List.java ์๋ธ๋ฆฟ ํธ์ถ
ํ๋ผ๋ฏธํฐ๋ก ๊ฒ์์ด ๋์ด์ค์ง์์ผ๋ฉด
//๋ชฉ๋ก
list = dao.select(c,n);
ํ๋ผ๋ฏธํฐ๋ก ๊ฒ์์ด ๋์ด์ค๋ฉด
//๊ฒ์๋ชฉ๋ก
list = dao.search(c,n,sc,sw);
list.java
๊ฒ์ ์์ ๋ ๋ถ๋ถ
// [๊ฒ์ ์ถ๊ฐ ์ฝ๋ฉ ๋ถ๋ถ] 1 S
int searchCondition = 1;
try { searchCondition=Integer.parseInt(request.getParameter("searchCondition")) ;}catch(Exception e) {}
// list.htm?searchWord= "" + // list.htm null
String searchWord = request.getParameter("searchWord") == null ? "" : request.getParameter("searchWord");
// [๊ฒ์ ์ถ๊ฐ ์ฝ๋ฉ ๋ถ๋ถ] 1 E
ArrayList<BoardDTO> list = null;
// ํ์ด์ง ์ฒ๋ฆฌ
int totalRecords, totalPages = 0;
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
PageBlock pageBlock = null;
try {
// [๊ฒ์ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 2 S
if( searchWord == "" ) {
list = dao.select(currentPage, numberPerPage);
}else {
list = dao.search(currentPage, numberPerPage, searchCondition, searchWord);
}
// [๊ฒ์ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 2 E
// ํ์ด์ง ์ฒ๋ฆฌ
pageBlock = pagingService(currentPage, numberPerPage, numberOfPageBlock , dao
, searchCondition, searchWord );
๊ฒ์์ํ๊ด๋ฆฌ js
// ๊ฒ์ ์ํ ๊ด๋ฆฌ
$("#searchCondition").val( '${ empty param.searchCondition? 1 : param.searchCondition }' );
$("#searchWord").val( '${ param.searchWord}' );
// [๊ฒ์ ํ์ด์ง ์ฒ๋ฆฌ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 3 S
if( searchWord == "" ) {
totalRecords = dao.getTotalRecords() ; // ์ ์ฒด ๋ ์ฝ๋ ์
totalPages = dao.getTotalPages( numberPerPage ) ; // ์ ์ฒด ํ์ด์ง ์
}else {
totalRecords = dao.getTotalRecords(searchCondition, searchWord); // ๊ฒ์๋ ๊ฒฐ๊ณผ์ ๋ ์ฝ๋ ์
totalPages = dao.getTotalPages( numberPerPage,searchCondition, searchWord ) ; // ๊ฒ์๋ ํ์ด์ง ์
}
// [๊ฒ์ ํ์ด์ง ์ฒ๋ฆฌ ์์ ์ฝ๋ฉ ๋ถ๋ถ] 3 E
// /jspPro/cstvsboard/list.htm?currentpage=2 + &searchCondition=1&searchWord=title+-+30
$(".pagination a:not(.active)").attr("href" , function (i,val){
return val + "&searchCondition=${ param.searchCondition}&searchWord=${ param.searchWord}";
});
๋ฌธ์ ์ 1
-> ๊ฒ์์ด์ ๊ฒ์์กฐ๊ฑด์ด ์๋๊ฒฝ์ฐ (ํ๋ผ๋ฏธํฐ ๋ค์ด์ฌ๊ฒฝ์ฐ) -> ๊ฒ์์ด๋ฅผ ๋ฌ๊ณ ํ์ด์ง 2๋ฒํ์ด์ง๋ฅผ ๋์ค๊ฒ ํ๊ธฐ ์ํด์
-> active ๊ฐ์ง์ง ์์ a -> ํ์ฌ ํ์ด์ง ์ ์ธ ์ฝ๋ฉ์ผ๋ก ํ๋ผ๋ฏธํฐ ์ ์ธ ๊ฒ์๋ฌ๊ณ ์๋๊ฑฐ ๋ถ์ด๊ฒ ๋ค๋๋ง
(2๋ฒํ์ด์ง ๋๋ฅผ๋ ๊ฒ์์กฐ๊ฑด์ 2๋ฒํ์ด์ง๊ธฐ ์ค์ ํ๊ธฐ์ํ ์ฝ๋ฉ)
๋ฌธ์ ์ 2
2) ๊ฒ์ํํ 2๋ฒํ์ด์ง ๋ณด๊ณ ์๋๋ฐ ํ๋์ ๊ฒ์๊ธ์ ํํ๊ณ ๋์๊ฐ๊ธฐ/ํ ๋๋ฅผ๋ ๊ฒ์ํ ๊ฒ์ 2๋ฒํ์ด์ง๋ก ๋์๊ฐ๊ฒํ๊ธฐ
(๊ณผ์ ..)
[๊ธ๋ชฉ๋ก] -> ํ๋์ ๊ฒ์๊ธ์ ์ ๋ชฉ ํด๋ฆญ -> ๊ธ ์์ธ๋ณด๊ธฐ
list.htm view.htm?seq=381 View.java view.jsp
list.jsp
view.jsp์์ [์์ , ์ญ์ ] ํด๋ฆญ -> /jspPro/cstvsboard/delete/htm?seq=340
delete.java ์๋ธ๋ฆฟ + doGet ๊ตฌํ
delete.java
package days05.board;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
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.util.DBConn;
@WebServlet("/cstvsboard/delete.htm")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
public Delete() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> Delete.doGet() ํธ์ถ๋จ...");
// delete.htm?seq=340
// ๋ฐ๋ก ์ญ์ ํ๊ณ ๋ ํ ๊ธ ๋ชฉ๋ก ์ด๋ X
// ์ญ์ ํ ๊ถํ X / ์์ฑ์ ํ์ธ ( ๋ก๊ทธ์ธ X, ๋น๋ฐ๋ฒํธ ์
๋ ฅ๋ฐ์์ .. )
String path = "/days05/board/delete.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
// delete.htm?seq=340
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// delete.jsp ํ์ด์ง์์ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ๊ณ ์ญ์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ...
// System.out.println("> Delete.doPost() ํธ์ถ๋จ...");
request.setCharacterEncoding("UTF-8");
int seq = Integer.parseInt( request.getParameter("seq") ); // ์ญ์ ํ ๊ธ ๋ฒํธ
String pwd = request.getParameter("pwd"); // ์
๋ ฅ๋ฐ์ ๋น๋ฐ๋ฒํธ
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
String originalPwd = dao.getOriginalPwd(seq);
if( originalPwd.equals(pwd) ) {
// ํด๋น ๊ฒ์๊ธ์ ์ญ์ ํ๊ณ ๋ชฉ๋ก ํ์ด์ง ์ผ๋ก ์ด๋.
rowCount = dao.delete(seq);
}else {
// ๋น๋ฐ๋ฒํธ๊ฐ ์๋ชป๋์๋ค๊ณ ๊ฒฝ๊ณ ์ฐฝ์ ๋์ฐ๊ณ ๋ค์ ๋น๋ฐ๋ฒํธ ์
๋ ฅํ๋ ์ญ์ ํ์ด์ง(delete.htm)๋ก ์ด๋.
request.setAttribute("error", "๋น๋ฐ๋ฒํธ๊ฐ ํ๋ฆฝ๋๋ค.");
doGet(request, response);
return ;
}
} catch (SQLException e) {
System.out.println("> Delete.doPost() Exception...");
e.printStackTrace();
}
DBConn.close();
// ํฌ์๋ฉ / [๋ฆฌ๋ค์ด๋ ํธ O ]
String location = "/jspPro/cstvsboard/list.htm";
if( rowCount == 1) {
location +="?delete=success";
}else {
location +="?delete=fail";
}
response.sendRedirect(location);
} // doPost
}
* ์ญ์ ํ ๊ถํ ์ฒดํฌ (์๋ ๊ด๋ฆฌ์, ์ฌ๊ธฐ์ ์์ฑ์ ํ์ธ - ๋ก๊ทธ์ธํ์ฌ๋๋ง)
* ๋น๋ฐ๋ฒํธ ์ ๋ ฅ๋ฐ์์ ์ญ์ ํ๋ ์์
delete.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 9. ์คํ 3:38:00</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table , td, th {
border:solid 1px gray;
}
table{
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
table{
width: 600px;
}
</style>
</head>
<body>
<h3>delete.jsp</h3>
<div align="center">
<h2>์ญ์ ํ๊ธฐ</h2>
<form method="post">
<table>
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ญ์ ํฉ๋๋ค</b></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td>
<input type="password" name="pwd" size="15" autofocus="autofocus">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์ญ์ ">
<input type="button" id="cancel" value="์ทจ์">
</td>
</tr>
</table>
</form>
</div>
<script>
<%
String pwdError = (String)request.getAttribute("error");
if( pwdError != null){
%>
alert( '<%= pwdError %>');
<%
}
%>
</script>
pwd ์ ๋ ฅ -> post -> delete.java doPost ๋น๋ฐ๋ฒํธ์ฒดํฌ
์ญ์ ํ ๋ชฉ๋ก ๋ณด๋ธ๋ค๋ฉด redirect (์๋ก์ด url ๋ ์ผํจ)
๋น๋ฒํ๋ฆฌ๋ค๋ฉด doGet์ผ๋ก ํฌ์๋ฉ(์ด๋ฏธ๊ตฌํ๋์ด์์) - request.setAttribute("error", '๋น๋ฐ๋ฒํธ๊ฐํ๋ฆฝ๋๋ค') ๋ฉ์ธ์ง ๋ฌ๊ณ ๊ฐ๊ธฐ
+ ์๋ฌ๋ฉ์ธ์ง delete.jsp ์์ ๋์ค๊ฒ
๊ธ์์
edit.java
package days05.board;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
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.util.DBConn;
@WebServlet("/cstvsboard/edit.htm")
public class Edit extends HttpServlet {
private static final long serialVersionUID = 1L;
public Edit() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> Edit.doGet() ํธ์ถ๋จ...");
// 1.
int seq = Integer.parseInt( request.getParameter("seq") ); // ์์ ํ ๊ธ๋ฒํธ
// 2.
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
BoardDTO dto = null;
try {
dto = dao.view(seq);
} catch (SQLException e) {
System.out.println("> Edit.doGet() Exception...");
e.printStackTrace();
}
request.setAttribute("dto", dto);
// 3. ํฌ์๋ฉ
String path = "/days05/board/edit.jsp"; // write.jsp ๋๊ฐ์..
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
// /jspPro/cstvsboard/edit.htm?seq=350 ๊ธ ์์
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
int seq = Integer.parseInt(request.getParameter("seq"));
// ์
๋ ฅ๋ฐ์ ์ ๊ฒ์๊ธ ์ ๋ณด๋ฅผ ์ป์ด์์
// DB insert~
// String writer = request.getParameter("writer") ;
String pwd = request.getParameter("pwd") ;
String email = request.getParameter("email") ;
String title = request.getParameter("title") ;
String content = request.getParameter("content") ;
int tag = Integer.parseInt( request.getParameter("tag") );
BoardDTO dto = new BoardDTO(seq, null, pwd, email, title, null, 0, tag, content);
Connection conn = DBConn.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
rowCount = dao.update(dto);
} catch (SQLException e) {
System.out.println("> Edit.doPost() Exception...");
e.printStackTrace();
}
DBConn.close();
// ์ ? [list.htm ๋ฆฌ๋ค์ด๋ ํธ O] / ํฌ์๋ฉ X
String location = "/jspPro/cstvsboard/view.htm?seq="+ seq;
if( rowCount == 1) {
location +="&edit=success";
}else {
location +="&edit=fail";
}
response.sendRedirect(location);
}
}
edit.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 3:28:25</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
</style>
</head>
<body>
<h3>edit.jsp</h3>
<div align="center">
<h2>๊ธ ์์ </h2>
<form method="post">
<table style="padding: 2px; width: 600px">
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์์ ํฉ๋๋ค.</b></td>
</tr>
<tr>
<td align="center">์ด๋ฆ</td>
<td><input type="text" name="writer" size="15" value="${ dto.writer }" disabled="disabled"></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td><input type="password" name="pwd" size="15" required="required"></td>
</tr>
<tr>
<td align="center">Email</td>
<td><input type="email" name="email" size="50" value="${ dto.email }"></td>
</tr>
<tr>
<td align="center">์ ๋ชฉ</td>
<td><input type="text" name="title" size="50" required="required" value="${ dto.title }" autofocus="autofocus"></td>
</tr>
<tr>
<td align="center">๋ด์ฉ</td>
<td><textarea name="content" cols="50" rows="10">${ dto.content }</textarea></td>
</tr>
<tr>
<td align="center">HTML</td>
<td>
<input type="radio" name="tag" value="1" >์ ์ฉ
<input type="radio" name="tag" value="0">๋น์ ์ฉ
<script>
$(":radio[name=tag][value=${ dto.tag }]").attr("checked", "checked");
</script>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์์ฑ ์๋ฃ">
<input type="button" onclick="history.back()" value="์ด์ ์ผ๋ก">
</td>
</tr>
</table>
</form>
</div>
<script>
$(function (){
$("form").submit(function (){
alert("์ ํจ์ฑ ๊ฒ์ฌ ์ฒดํฌ ํ๋ค. ");
// event.preventDefault();
// return false;
});
});
</script>
</body>
</html>
* Post๋ก ๋ณด๋ผ์ ๋๊ฐ์ ๊ฐ์ด ๋์ด๊ฐ๊ธฐ ๋๋ฌธ์ request๋ก seq๋ฐ์์ฌ ์ ์์
* view๋ก ๋๊ฒจ์ฃผ๊ณ (๋ฆฌ๋ค์ด๋ ํธ - view๋ก ์ด๋์ํด)
* view์์ ์์ ์๋ฃ ์๋๋์ค๊ฒ
[๋ชจ๋ฌ์ฐฝ์ผ๋ก ์ญ์ ]
view.jsp
<!-- ์ญ์ ๋ชจ๋ฌ์ฐฝ div ํ๊ทธ -->
<div id="dialog-form" align="center" title="์ญ์ ">
<h2>์ญ์ ํ๊ธฐ</h2>
<form method="post" action="<%= contextPath%>/cstvsboard/delete.htm?seq=${ param.seq}">
<table>
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ญ์ ํฉ๋๋ค</b></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td>
<input type="password" name="pwd" size="15" autofocus="autofocus">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์ญ์ ">
<input type="button" id="cancel" value="์ทจ์">
</td>
</tr>
</table>
</form>
</div>
<script>
// 1.
dialog = $("#dialog-form").dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
buttons:{},
close: function (){
form[0].reset();
}
});
//2.
form = dialog.find("form");
// 3. ๋ชจ๋ฌ์ฐฝ ์ด๊ธฐ
$("#btnModalDelete").on("click", function(event) {
dialog.dialog("open" );
})
// 4. ๋ชจ๋ฌ์ฐฝ ๋ซ๊ธฐ
$("#cancel").on("click", function(event) {
dialog.dialog("close" );
})
</script>
์ต๊ทผ๋๊ธ