[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="์ž‘์„ฑ ์™„๋ฃŒ">
				  &nbsp;&nbsp;&nbsp; 
				  <input type="reset" value="๋‹ค์‹œ ์ž‘์„ฑ">
				  &nbsp;&nbsp;&nbsp; 
				  <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"> &raquo; </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="์‚ญ์ œ">&nbsp;&nbsp;
	      <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"> &raquo; </a>
             --%>  
     
             <c:if test="${ pageBlock.prev }">
                  <a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${  pageBlock.startOfPageBlock - 1  }"> &laquo; </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  }"> &raquo; </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="์‚ญ์ œ">&nbsp;&nbsp;
	      <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="์ž‘์„ฑ ์™„๋ฃŒ">
				  &nbsp;&nbsp;&nbsp;				  
				  <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="์‚ญ์ œ">&nbsp;&nbsp;
	      <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>
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ