<boardContent.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
pageContext.setAttribute("newLine", "\n");
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<jsp:include page = "header.jsp"/>
<h2>MVC Framework</h2>
<div class="panel panel-default">
<div class="panel-heading">BOARD</div>
<div class="panel-body">Panel Content
<table class="table table-bordered">
<tr>
<td>제목</td>
<td>${vo.title}</td>
</tr>
<tr>
<td>내용</td>
<td>${fn:replace(vo.content,newLine,"<br/>")}</td>
</tr>
<tr>
<td>작성자</td>
<td>${vo.writer}</td>
</tr>
<tr>
<td>작성일</td>
<td>${vo.indate}</td>
</tr>
<tr>
<td colspan="2" align="center">
<a href="boardUpdateForm.do?idx=${vo.idx}" class="btn btn-primary btn-sm">수정</a>
<a href="boardDelete.do?idx=${vo.idx}" class="btn btn-warning btn-sm">삭제</a>
<a href="boardList.do" class="btn btn-info btn-sm">목록</a>
</td>
</tr>
</table>
</div>
<div class="panel-footer">지능형 IoT_최지연</div>
</div>
</div>
</body>
</html>
<boardUpdate.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<jsp:include page = "header.jsp"/>
<h2>MVC Framework</h2>
<div class="panel panel-default">
<div class="panel-heading">BOARD</div>
<div class="panel-body">
<form action="boardUpdate.do" method="post">
<input type="hidden" name="idx" value="${vo.idx}"/>
<table class="table table-borderd">
<tr>
<td>제목</td>
<td><input type="text" name="title" class="form-control" value="${vo.title}"/></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="7" class="form-control" name="content">${vo.content}</textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit" class="btn btn-primary btn-sm">수정</button>
<button type="reset" class="btn btn-warning btn-sm">취소</button>
<button type="button" class="btn btn-info btn-sm" onclick="location.href='boardList.do'">목록</button>
</td>
</tr>
</table>
</form>
</div>
<div class="panel-footer">지능형 IoT_최지연</div>
</div>
</div>
</body>
</html>
<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="kr.board.dao.BoardDAO" >
<select id="getAllList" resultType="Board">
select * from board order by idx desc
</select>
<insert id="boardInsert" parameterType="Board">
insert into board(idx, title, content, writer)
values(board_idx.nextval,#{title},#{content},#{writer})
</insert>
<select id="boardContent" resultType="Board">
select * from board where idx=#{idx}
</select>
<delete id="boardDelete">
delete from board where idx=#{idx}
</delete>
<update id="boardUpdate" parameterType="Board">
update board set title=#{title}, content=#{content} where idx=#{idx}
</update>
</mapper>
<boardDeletecontroller.java>
package kr.board.controller;
import java.io.IOException;
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 kr.board.dao.BoardDAO;
@WebServlet("/boardDelete.do")
public class BoardDeleteController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ?idx=10
int idx = Integer.parseInt(request.getParameter("idx"));
BoardDAO dao = new BoardDAO();
dao.boardDelete(idx);
response.sendRedirect("boardList.do");
}
}
<boardUpdatecontroller.java>
package kr.board.controller;
import java.io.IOException;
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 kr.board.dao.BoardDAO;
import kr.board.domain.Board;
@WebServlet("/boardUpdate.do")
public class BoardUpdateController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String content = request.getParameter("content");
int idx = Integer.parseInt(request.getParameter("idx"));
Board vo = new Board();
vo.setTitle(title);
vo.setContent(content);
vo.setIdx(idx);
BoardDAO dao = new BoardDAO();
dao.boardUpdate(vo); //등록
response.sendRedirect("boardList.do");
}
}
<boardListcontroller.java>
package kr.board.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 kr.board.dao.BoardDAO;
import kr.board.domain.Board;
@WebServlet("/boardList.do")
public class BoardListController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // 게시판 전체 리스트를 가져오기(Model-DAO)
// List<Board> list = new ArrayList<Board>();
// Board vo1 = new Board(1,"게시판 테스트","게시판 테스트","관리자","2022-05-24", 0);
// Board vo2 = new Board(2,"게시판 테스트","게시판 테스트","김운비","2022-05-24", 0);
// Board vo3 = new Board(3,"게시판 테스트","게시판 테스트","박매일","2022-05-24", 0);
// list.add(vo1);
// list.add(vo2);
// list.add(vo3);
// Model과 연동하기
BoardDAO dao = new BoardDAO();
List<Board> list = dao.getAllList();
// 객체바인딩(*****)
request.setAttribute("list", list);
// boardList.jsp
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/board/boardList.jsp");
rd.forward(request, response);
}
}
<BoardDAO.java>
package kr.board.dao;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import kr.board.domain.Board;
// JDBC -> MyBatis(Connection POOL) ->ORM(HIbernate) -> JPA
public class BoardDAO {
// 1. Connection POOL -> SqlSessionFactory
// SqlSessionFactoryBuilder
// SqlSessionFactory
// SqlSession
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "kr/board/dao/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}//_db_
public List<Board> getAllList(){
// BoardMapper.xml(SQL)
// MemberMapper.xml(SQL)
SqlSession session = sqlSessionFactory.openSession();
List<Board> list = session.selectList("getAllList");
session.close(); // 반납(*)
return list;
}
public void boardInsert(Board vo) {
SqlSession session=sqlSessionFactory.openSession();
session.insert("boardInsert", vo);
session.commit(); // insert, delete, update : db내용 바뀜
session.close();
}
public Board boardContent(int idx){
SqlSession session=sqlSessionFactory.openSession();
Board vo = session.selectOne("boardContent", idx);
session.close(); // select : db내용 안바뀌니 commit 안해줘도 됨
return vo;
}
public void boardDelete(int idx) {
SqlSession session=sqlSessionFactory.openSession();
session.delete("boardDelete", idx);
session.commit();
session.close();
}
public void boardUpdate(Board vo) {
SqlSession session=sqlSessionFactory.openSession();
session.update("boardUpdate", vo);
session.commit();
session.close();
}
}
'Maven Project' 카테고리의 다른 글
게시판 세부내용 (0) | 2022.05.27 |
---|---|
게시글 목록 조회 (0) | 2022.05.27 |
<게시판 - 파일 업로드 실습> (0) | 2022.05.26 |
게시판 만들기 (0) | 2022.05.25 |
JSTL 사용법 (0) | 2022.05.25 |