<boardMain.jsp>
<%@page import="com.smhrd.model.BoardDAO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.smhrd.model.BoardVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 게시글 목록 기능 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Forty by HTML5 UP</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assetsBoard/css/main.css" />
<link rel="stylesheet" href="assetsBoard/css/board.css" />
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<body>
<%
BoardDAO dao = new BoardDAO();
ArrayList<BoardVO> list = dao.showBoard();
%>
<div id="board">
<table id = "list">
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>시간</td>
<td>삭제</td>
</tr>
<%for(int i=0; i<list.size();i++) {%>
<tr>
<td><%= i+1 %></td>
<td><a href="viewBoard.jsp?num=<%=list.get(i).getNum()%>"><%= list.get(i).getTitle() %></a></td>
<td><%= list.get(i).getWriter() %></td>
<td><%= list.get(i).getB_day() %></td>
<td><a href ="DeleteBoardService?num=<%=list.get(i).getNum()%>">삭제</a></td>
</tr>
<%} %>
<!-- <%for(BoardVO vo : list) {%>
<tr>
<td><%=vo.getTitle() %></td>
<td><%=vo.getWriter() %></td>
<td><%=vo.getB_day() %></td>
</tr>
<%} %> -->
</table>
<a href="main.jsp"><button id="writer">홈으로가기</button></a>
<a href="writerBoard.jsp"><button id="writer">작성하러가기</button></a>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
</html>
<DeleteBoardService.java>
package com.smhrd.controller;
import java.io.IOException;
import java.net.URLEncoder;
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.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import com.smhrd.model.BoardDAO;
import com.smhrd.model.BoardVO;
@WebServlet("/DeleteBoardService")
public class DeleteBoardService extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 쿼리스트링으로 보낸값 받아오기
int num = Integer.parseInt(request.getParameter("num"));
BoardDAO dao = new BoardDAO();
int row = dao.boardDelete(num);
RequestDispatcher rd = request.getRequestDispatcher("boardMain.jsp");
rd.forward(request, response);
}
}
<BoardDAO.java>
package com.smhrd.model;
import java.util.ArrayList;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.smhrd.database.SqlSessionManager;
public class BoardDAO {
private SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession();
private SqlSession sqlSession = null;
// 파일업로드 메소드
public int upload(BoardVO vo) {
int row = 0;
try {
sqlSession = sqlSessionFactory.openSession(true);
row = sqlSession.insert("com.smhrd.model.BoardDAO.boardinsert",vo);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return row;
}
// 게시글 목록 조회 메소드
@SuppressWarnings({"unchecked","rawtypes"})
public ArrayList<BoardVO> showBoard() {
ArrayList<BoardVO> list = new ArrayList<BoardVO>();
try {
sqlSession = sqlSessionFactory.openSession(true);
list = (ArrayList) sqlSession.selectList("com.smhrd.model.BoardDAO.boardselect");
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return list;
}
// 게시글 세부내용 메소드
public BoardVO showDetail(int num) {
BoardVO vo = null;
try {
sqlSession = sqlSessionFactory.openSession(true);
vo = sqlSession.selectOne("com.smhrd.model.BoardDAO.boarddetail",num);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return vo;
}
// 게시글 삭제 메소드
public int boardDelete(int num) {
int row = 0;
try {
sqlSession = sqlSessionFactory.openSession(true);
row=sqlSession.delete("com.smhrd.model.BoardDAO.boarddelete",num);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return row ;
}
}
<BoardMapper.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="com.smhrd.model.BoardDAO" >
<insert id="boardinsert" parameterType="BoardVO">
insert into messageboard values(num_board.nextval, #{title}, #{writer}, #{fileName}, #{content}, sysdate)
</insert>
<select id="boardselect" resultType="BoardVO">
select * from messageboard order by b_day desc
</select>
<select id="boarddetail" parameterType="int" resultType="BoardVO">
select * from messageboard where num = #{num}
</select>
<delete id="boarddelete">
delete from messageboard where num = #{num}
</delete>
</mapper>
'Maven Project' 카테고리의 다른 글
FrontController-command 방식 (0) | 2022.05.30 |
---|---|
MVC Framework 게시판 (0) | 2022.05.27 |
게시판 세부내용 (0) | 2022.05.27 |
게시글 목록 조회 (0) | 2022.05.27 |
게시판 삭제, 게시판 글 수정 (0) | 2022.05.26 |