삭제 기능!!
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="${pageContext.request.contextPath}/resources/js/httpRequest.js"></script>
<script>
function reply(f){
location.href="reply_form.do?idx="+f.idx.value+"&ref="+f.ref.value;
}
function del(f){
if(confirm("삭제하시겠습니까?")){
if(f.c_pwd.value != f.pwd.value) {
alert("비밀번호 불일치");
return;
}
let url = "del.do?idx="+f.idx.value;
sendRequest(url, null, resultFun, "post");
}
}
function resultFun(){
if(xhr.readyState ==4 &&xhr.status==200){
var data=xhr.responseText;
if(data=="yes"){
alert("삭제성공");
location.href="list.do";
}else{
alert("삭제실패");
}
}
}
</script>
</head>
<body>
<form>
<input type="hidden" name="idx" value="${vo.idx}">
<input type="hidden" name="ref" value="${vo.ref}">
<input type="hidden" name="pwd" value="${vo.pwd}">
<table border="1" width="600">
<tr>
<th>제목</th>
<td>${vo.subject}</td>
</tr>
<tr>
<th>작성자</th>
<td>${vo.name}</td>
</tr>
<tr>
<th>작성일</th>
<td>${vo.regdate}</td>
</tr>
<tr>
<td colspan="2"><pre>${vo.content}</pre></td>
</tr>
<tr>
<th>비밀번호</th>
<td>
<input type="password" name="c_pwd">
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="목록으로" onclick="location.href='list.do'">
<c:if test="${vo.depth lt 1 }">
<!-- eq - equal ( = )ne - not equal ( <> )lt - little ( < )
le - little or equal ( <= )gt - greater ( > )ge - greater or equal ( >= ) -->
<input type="button" value="댓글달기" onclick="reply(this.form);">
</c:if>
<input type="button" value="삭제" onclick="del(this.form);">
</td>
</tr>
</table>
</form>
</body>
</html>
*상대경로가 안될경우 절대경로로 설정해준다.
BoardController 수정
package com.korea.board;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import dao.BoardDAO;
import util.Util;
import vo.BoardVO;
@Controller
public class BoardController {
@Autowired
HttpServletRequest request;
BoardDAO board_dao;
public void setBoard_dao(BoardDAO board_dao) {
this.board_dao = board_dao;
}
//전체 게시글 조회
@RequestMapping(value={"/", "/list.do"})
public String seletList(Model model){
List<BoardVO> list= board_dao.selectList();
model.addAttribute("list", list);
//show라는 이름으로 저장된 값을 제거
request.getSession().removeAttribute("show");
return Util.Board.VIEW_PATH+ "board_list.jsp";
}
//새글 작성을 위한 페이지로 전환
@RequestMapping("/insert_form.do")
public String insert_form() {
return Util.Board.VIEW_PATH + "board_write.jsp";
}
//새글쓰기
@RequestMapping("/insert.do")
public String insert(BoardVO vo) {
//ip가져오기
String ip=request.getRemoteAddr();
vo.setIp(ip);
int res= board_dao.insert(vo);
return "redirect:list.do";
}
//게시글 상세보기
@RequestMapping("/view.do")
public String view(Model model, int idx) {
BoardVO vo =board_dao.selectOne(idx);
model.addAttribute("vo", vo);
HttpSession session = request.getSession();
String show =(String)session.getAttribute("show");
if (show == null) {
//조회수 증가를 위한 업데이트메서드
board_dao.update_readhit(idx);
session.setAttribute("show", "");
}
return Util.Board.VIEW_PATH + "board_view.jsp";
}
//댓글 작성을 위한 페이지로 전환
@RequestMapping("/reply_form.do")
public String replyForm(Model model, int idx, int ref) {
model.addAttribute("idx", idx);
model.addAttribute("ref", ref);
return Util.Board.VIEW_PATH + "board_reply.jsp";
}
//댓글쓰기
@RequestMapping("/reply.do")
public String reply(BoardVO vo) {
//ip가져오기
String ip=request.getRemoteAddr();
vo.setIp(ip);
BoardVO baseVo=board_dao.selectOne(vo.getIdx());
//댓글작성을 위한 기준글의 step이상인 값은 +1처리를 해주자
board_dao.update_step(baseVo);
//댓글이 들어갈 위치를 설정
vo.setStep(baseVo.getStep()+1);
vo.setDepth(baseVo.getDepth()+1);
//댓글 추가
board_dao.reply(vo);
return "redirect:list.do";
}
//게시글 삭제(된것처럼) 업데이트
@RequestMapping("/del.do")
@ResponseBody
public String del(int idx) {
BoardVO vo =board_dao.selectOne(idx);
vo.setSubject("삭제된 게시글입니다");
vo.setName("unknown");
int res = board_dao.delete(vo);
if (res==1) {
return "yes";
}else {
return "no";
}
}
}
BoardDAO 수정
package dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import vo.BoardVO;
public class BoardDAO {
SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
//전체 게시글 조회
public List<BoardVO> selectList(){
List<BoardVO> list = sqlSession.selectList("b.board_list");
return list;
}
//새글추가
public int insert(BoardVO vo) {
int res=sqlSession.insert("b.board_insert", vo);
return res;
}
//상세보기를 위한 게시글 한 건 조회
public BoardVO selectOne(int idx) {
BoardVO vo=sqlSession.selectOne("b.board_one", idx);
return vo;
}
//조회수 증가
public int update_readhit(int idx) {
int res = sqlSession.update("b.update_readhit",idx);
return res;
}
//댓글 작성을 위한 step값 증가
public int update_step(BoardVO vo) {
int res=sqlSession.update("b.update_step", vo);
return res;
}
//댓글 작성
public int reply(BoardVO vo) {
int res =sqlSession.insert("b.board_reply", vo);
return res;
}
//댓글삭제(된것처럼업데이트)
public int delete(BoardVO vo) {
int res= sqlSession.update("b.del_update", vo);
return res;
}
}
board.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="b">
<select id="board_list" resultType="board">
select * from sboard order by ref DESC, step ASC
</select>
<!-- 새글 추가 -->
<insert id="board_insert" parameterType="board">
insert into sboard values(
seq_sboard_idx.nextVal,
#{name},
#{subject},
#{content},
#{pwd},
#{ip},
sysdate,
0,
seq_sboard_idx.currVal,
0,
0,
0
)
</insert>
<!-- 상세보기 -->
<select id="board_one" parameterType="int" resultType="board">
select * from sboard where idx=#{idx}
</select>
<!-- 조회수 증가 -->
<update id="update_readhit" parameterType="int">
<!-- idx에 해당되는 게시글의 readhit를 1씩 증가 -->
update sboard set readhit = readhit + 1
where idx=#{idx}
</update>
<!-- step 값 증가 -->
<update id="update_step" parameterType="board">
update sboard set step = step + 1
where ref=#{ref} and step > #{step}
</update>
<!-- 댓글쓰기 -->
<insert id="board_reply" parameterType="board">
insert into sboard values(
seq_sboard_idx.nextVal,
#{name},
#{subject},
#{content},
#{pwd},
#{ip},
sysdate,
0,
#{ref},
#{step},
#{depth},
0
)
</insert>
<!-- 삭제 -->
<update id="del_update" parameterType="board">
update sboard set subject =#{subject},
name=#{name},
del_info = -1
where idx=#{idx}
</update>
</mapper>
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="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
a{text-decoration:none;}
</style>
</head>
<body>
<table border="1" width="700" align="center">
<tr>
<th>번호</th>
<th width="350">제목</th>
<th>작성자</th>
<th>작성일</th>
<th>조회수</th>
</tr>
<c:forEach var="vo" items="${list}">
<tr>
<td align="center">${vo.idx }</td>
<td>
<!-- 댓글일경우 제목을 들여쓰기한다 -->
<c:forEach begin="1" end="${vo.depth }"> </c:forEach>
<!-- 댓글기호 -->
<c:if test="${vo.depth ne 0 }">ㄴ</c:if>
<!-- 삭제된 글일경우 클릭이 불가 -->
<c:if test="${vo.del_info eq 0 }">
<a href="view.do?idx=${vo.idx}"><font color="black">${vo.subject}</font></a></td>
</c:if>
<c:if test="${vo.del_info eq -1 }">
<a href=""><font color="gray">${vo.subject}</font></a></td>
</c:if>
<td align="center">${vo.name }</td>
<td align="center">${fn:split(vo.regdate, ' ')[0]}</td>
<td align="center">${vo.readhit }</td>
</tr>
</c:forEach>
<tr>
<td colspan="5" align="center">
< 1 2 3 >
</td>
</tr>
<tr>
<td colspan="5" align="right">
<input type="button" value="새글작성" onclick="location.href='insert_form.do'"
style="cusor:pointer">
</td>
</tr>
</table>
</body>
</html>

페이징 처리!!!
utill 클래스 수정
package util;
public class Util {
public static class Board{
public static String VIEW_PATH="/WEB-INF/views/board/";
//한 페이지에 보여줄 게시물 수
public static final int BLOCKLIST =5;
// < 1 2 3 > 한 화면에 보여질 페이지 메뉴의 수
public static final int BLOCKPAGE =3;
}
public static class Bbs{
public static String VIEW_PATH="/WEB-INF/views/bbs/";
}
}
BoardController 수정
package com.korea.board;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import dao.BoardDAO;
import util.Paging;
import util.Util;
import vo.BoardVO;
@Controller
public class BoardController {
@Autowired
HttpServletRequest request;
BoardDAO board_dao;
public void setBoard_dao(BoardDAO board_dao) {
this.board_dao = board_dao;
}
//전체 게시글 조회
@RequestMapping(value={"/", "/list.do"})
public String seletList(Model model, String page){
int nowPage =1;
if(page != null) {
nowPage = Integer.parseInt(page);
}
//한 페이지에 표시될 게시물의 시작과 끝 변수를 계산
//?page=1
int start = (nowPage - 1) * Util.Board.BLOCKLIST + 1 ;
int end = start +Util.Board.BLOCKLIST - 1;
//map에 시작변수와 끝 번호를 저장
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("start", start);
map.put("end", end);
List<BoardVO> list= board_dao.selectList(map);
model.addAttribute("list", list);
//전체게시물 수 조회
int row_total = board_dao.getRowTotal();
//하단의 표기될 페이지 메뉴 생성
String pageMenu = Paging.getPaging(
"list.do", nowPage, row_total, Util.Board.BLOCKLIST,
Util.Board.BLOCKPAGE);
model.addAttribute("pageMenu", pageMenu);
//show라는 이름으로 저장된 값을 제거
request.getSession().removeAttribute("show");
return Util.Board.VIEW_PATH+ "board_list.jsp";
}
//새글 작성을 위한 페이지로 전환
@RequestMapping("/insert_form.do")
public String insert_form() {
return Util.Board.VIEW_PATH + "board_write.jsp";
}
//새글쓰기
@RequestMapping("/insert.do")
public String insert(BoardVO vo) {
//ip가져오기
String ip=request.getRemoteAddr();
vo.setIp(ip);
int res= board_dao.insert(vo);
return "redirect:list.do";
}
//게시글 상세보기
@RequestMapping("/view.do")
public String view(Model model, int idx) {
BoardVO vo =board_dao.selectOne(idx);
model.addAttribute("vo", vo);
HttpSession session = request.getSession();
String show =(String)session.getAttribute("show");
if (show == null) {
//조회수 증가를 위한 업데이트메서드
board_dao.update_readhit(idx);
session.setAttribute("show", "");
}
return Util.Board.VIEW_PATH + "board_view.jsp";
}
//댓글 작성을 위한 페이지로 전환
@RequestMapping("/reply_form.do")
public String replyForm(Model model, int idx, int ref, int page) {
model.addAttribute("idx", idx);
model.addAttribute("ref", ref);
//model.addAttribute("page", page);
return Util.Board.VIEW_PATH + "board_reply.jsp?page="+page;
}
//댓글쓰기
@RequestMapping("/reply.do")
public String reply(BoardVO vo, String page) {
//ip가져오기
String ip=request.getRemoteAddr();
vo.setIp(ip);
BoardVO baseVo=board_dao.selectOne(vo.getIdx());
//댓글작성을 위한 기준글의 step이상인 값은 +1처리를 해주자
board_dao.update_step(baseVo);
//댓글이 들어갈 위치를 설정
vo.setStep(baseVo.getStep()+1);
vo.setDepth(baseVo.getDepth()+1);
//댓글 추가
board_dao.reply(vo);
return "redirect:list.do?page="+page;
}
//게시글 삭제(된것처럼) 업데이트
@RequestMapping("/del.do")
@ResponseBody
public String del(int idx) {
BoardVO vo =board_dao.selectOne(idx);
vo.setSubject("삭제된 게시글입니다");
vo.setName("unknown");
int res = board_dao.delete(vo);
if (res==1) {
return "yes";
}else {
return "no";
}
}
}
BoardDAO 수정
package dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import vo.BoardVO;
public class BoardDAO {
SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
//전체 게시글 조회
public List<BoardVO> selectList(Map<String, Integer> map){
List<BoardVO> list = sqlSession.selectList("b.board_list", map);
return list;
}
//새글추가
public int insert(BoardVO vo) {
int res=sqlSession.insert("b.board_insert", vo);
return res;
}
//상세보기를 위한 게시글 한 건 조회
public BoardVO selectOne(int idx) {
BoardVO vo=sqlSession.selectOne("b.board_one", idx);
return vo;
}
//조회수 증가
public int update_readhit(int idx) {
int res = sqlSession.update("b.update_readhit",idx);
return res;
}
//댓글 작성을 위한 step값 증가
public int update_step(BoardVO vo) {
int res=sqlSession.update("b.update_step", vo);
return res;
}
//댓글 작성
public int reply(BoardVO vo) {
int res =sqlSession.insert("b.board_reply", vo);
return res;
}
//댓글삭제(된것처럼업데이트)
public int delete(BoardVO vo) {
int res= sqlSession.update("b.del_update", vo);
return res;
}
//전체 게시물 수 조회
public int getRowTotal() {
int count =sqlSession.selectOne("b.board_count");
return count;
}
}
board.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="b">
<select id="board_list" resultType="board"
parameterType="java.util.Map">
select * from
(select rank() over(order by ref desc, step asc) no, b.* from sboard b )
where no between #{start} and #{end}
</select>
<!-- 새글 추가 -->
<insert id="board_insert" parameterType="board">
insert into sboard
values(
seq_sboard_idx.nextVal,
#{name},
#{subject},
#{content},
#{pwd},
#{ip},
sysdate,
0,
seq_sboard_idx.currVal,
0,
0,
0
)
</insert>
<!-- 상세보기 -->
<select id="board_one" parameterType="int" resultType="board">
select *
from sboard where idx=#{idx}
</select>
<!-- 조회수 증가 -->
<update id="update_readhit" parameterType="int">
<!-- idx에 해당되는 게시글의 readhit를 1씩 증가 -->
update sboard set readhit = readhit + 1
where idx=#{idx}
</update>
<!-- step 값 증가 -->
<update id="update_step" parameterType="board">
update sboard set step =
step + 1
where ref=#{ref} and step > #{step}
</update>
<!-- 댓글쓰기 -->
<insert id="board_reply" parameterType="board">
insert into sboard
values(
seq_sboard_idx.nextVal,
#{name},
#{subject},
#{content},
#{pwd},
#{ip},
sysdate,
0,
#{ref},
#{step},
#{depth},
0
)
</insert>
<!-- 삭제 -->
<update id="del_update" parameterType="board">
update sboard set subject
=#{subject},
name=#{name},
del_info = -1
where idx=#{idx}
</update>
<!-- 전체게시물 수 -->
<select id="board_count" resultType="int">
select COUNT(*) from sboard
</select>
</mapper>
utill 패키지에 파일 추가
Paging.java 를 util 패키지에 추가
board_list수정
<%@ 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="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
a{text-decoration:none;}
</style>
</head>
<body>
<table border="1" width="700" align="center">
<tr>
<th>번호</th>
<th width="350">제목</th>
<th>작성자</th>
<th>작성일</th>
<th>조회수</th>
</tr>
<c:forEach var="vo" items="${list}">
<tr>
<td align="center">${vo.idx }</td>
<td>
<!-- 댓글일경우 제목을 들여쓰기한다 -->
<c:forEach begin="1" end="${vo.depth }"> </c:forEach>
<!-- 댓글기호 -->
<c:if test="${vo.depth ne 0 }">ㄴ</c:if>
<!-- 삭제된 글일경우 클릭이 불가 -->
<c:if test="${vo.del_info eq 0 }">
<a href="view.do?idx=${vo.idx}&page=${empty param.page ? 1 : param.page }"><font color="black">${vo.subject}</font></a></td>
</c:if>
<c:if test="${vo.del_info eq -1 }">
<a href=""><font color="gray">${vo.subject}</font></a></td>
</c:if>
<td align="center">${vo.name }</td>
<td align="center">${fn:split(vo.regdate, ' ')[0]}</td>
<td align="center">${vo.readhit }</td>
</tr>
</c:forEach>
<tr>
<td colspan="5" align="center">
${ pageMenu }
</td>
</tr>
<tr>
<td colspan="5" align="right">
<input type="button" value="새글작성" onclick="location.href='insert_form.do'"
style="cusor:pointer">
</td>
</tr>
</table>
</body>
</html>
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="${pageContext.request.contextPath}/resources/js/httpRequest.js"></script>
<script>
function reply(f){
location.href="reply_form.do?idx="+f.idx.value+"&ref="+f.ref.value+"&page=${param.page}";
}
function del(f){
if(confirm("삭제하시겠습니까?")){
if(f.c_pwd.value != f.pwd.value) {
alert("비밀번호 불일치");
return;
}
let url = "del.do?idx="+f.idx.value;
sendRequest(url, null, resultFun, "post");
}
}
function resultFun(){
if(xhr.readyState ==4 &&xhr.status==200){
var data=xhr.responseText;
if(data=="yes"){
alert("삭제성공");
location.href="list.do?page=${param.page}";
}else{
alert("삭제실패");
}
}
}
</script>
</head>
<body>
<form>
<input type="hidden" name="idx" value="${vo.idx}">
<input type="hidden" name="ref" value="${vo.ref}">
<input type="hidden" name="pwd" value="${vo.pwd}">
<table border="1" width="600">
<tr>
<th>제목</th>
<td>${vo.subject}</td>
</tr>
<tr>
<th>작성자</th>
<td>${vo.name}</td>
</tr>
<tr>
<th>작성일</th>
<td>${vo.regdate}</td>
</tr>
<tr>
<td colspan="2"><pre>${vo.content}</pre></td>
</tr>
<tr>
<th>비밀번호</th>
<td>
<input type="password" name="c_pwd">
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="목록으로" onclick="location.href='list.do?page=${param.page}'">
<c:if test="${vo.depth lt 1 }">
<!-- eq - equal ( = )ne - not equal ( <> )lt - little ( < )
le - little or equal ( <= )gt - greater ( > )ge - greater or equal ( >= ) -->
<input type="button" value="댓글달기" onclick="reply(this.form);">
</c:if>
<input type="button" value="삭제" onclick="del(this.form);">
</td>
</tr>
</table>
</form>
</body>
</html>
board_reply.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>
<script>
function send_check(f){
//유효성 체크 했다고 하고
//비밀번호 유효성 체크
var pwdPattern = /^[A-Za-z0-9]{4,10}$/;
//영문과 숫자로 이루어진 비밀번호
if(!pwdPattern.test(f.pwd.value)){
alert("비밀번호 형식을 확인하세요");
return;
}
f.submit();
}
</script>
</head>
<body>
<form method="post" action="reply.do">
<input type="hidden" name="page" value="${param.page }">
<input type="hidden" name="ref" value="${ref }">
<input type="hidden" name="idx" value="${idx }">
<table border="1" align="center" width="610">
<caption>:::댓글 쓰기:::</caption>
<tr>
<th>제목</th>
<td><input name="subject" style="width:300px"></td>
</tr>
<tr>
<th>작성자</th>
<td><input name="name" style="width:300px"></td>
</tr>
<tr>
<th>내용</th>
<td><textarea name="content" rows="10" cols="70"></textarea></td>
</tr>
<tr>
<th>비밀번호</th>
<td><input name="pwd" type="password" style="width:300px"></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="등록" onclick="send_check(this.form);">
<input type="button" value="취소" onclick="location.href='list.do?page=${param.page}'">
</td>
</tr>
</table>
</form>
</body>
</html>

페이징 처리까지 완료!!!
'SPRING' 카테고리의 다른 글
| [SPRING] 09/16 게시판만들기-2 (0) | 2022.09.16 |
|---|---|
| [SPRING] 0915 게시판 만들기-1 (0) | 2022.09.15 |
| [SPRING] 09/14 방명록에 사진 첨부 (0) | 2022.09.14 |
| [SPRING] 09/13 글쓰기 기능, 수정 추가 (0) | 2022.09.13 |
| [SPRING] 09/08 (0) | 2022.09.08 |
댓글