<boardList.jsp>
<%@page import="kr.board.domain.Board"%>
<%@page import="java.util.List"%>
<%@ 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 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">
<table class="table table-bordered">
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>작성일</td>
<td>조회수</td>
</tr>
<c:forEach var="vo" items="${list}">
<tr>
<td>${vo.idx}</td>
<td>${vo.title}</td>
<td>${vo.content}</td>
<td>${vo.writer}</td>
<td>${vo.count}</td>
</tr>
</c:forEach>
</table>
<!--똑같음 : <button class="btn btn-success btn-sm">글쓰기</button> -->
<a href="boardForm.do" class="btn btn-success btn-sm">글쓰기</a>
</div>
<div class="panel-footer">지능형 IoT_최지연</div>
</div>
</div>
</body>
</html>
<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("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();
session.close();
}
}
<BoardForm.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="boardInsert.do" method="post">
<table class="table">
<tr>
<td>제목</td>
<td><input type="text" name="title" class="form-control"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="7" cols="20" name="content" class="form-control"></textarea></td>
</tr>
<tr>
<td>작성자</td>
<td><input type="text" name="writer" class="form-control"></td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit" class="btn btn-info btn-sm">등록</button>
<button type="reset" class="btn btn-warning btn-sm">취소</button>
</td>
</tr>
</table>
</form>
</div>
<div class="panel-footer">지능형 IoT_최지연</div>
</div>
</div>
</body>
</html>
<BoardFormController.java>
package kr.board.controller;
import java.io.IOException;
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;
@WebServlet("/boardForm.do")
public class BoardFormController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("board/boardForm.jsp");
rd.forward(request, response);
}
}
<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(title, content, writer)
values(#{title},#{content},#{writer})
</insert>
</mapper>
<BoardInsertController.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("/boardInsert.do")
public class BoardInsertController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 파라메터수집(VO)
String title = request.getParameter("title");
String content = request.getParameter("content");
String writer = request.getParameter("writer");
Board vo = new Board();
vo.setTitle(title);
vo.setContent(content);
vo.setWriter(writer);
BoardDAO dao = new BoardDAO();
dao.boardInsert(vo); //등록
// 등록후에는 다시 리스트페이지로 ... boardList.do
response.sendRedirect("boardList.do");
}
}
'Maven Project' 카테고리의 다른 글
게시판 삭제, 게시판 글 수정 (0) | 2022.05.26 |
---|---|
<게시판 - 파일 업로드 실습> (0) | 2022.05.26 |
JSTL 사용법 (0) | 2022.05.25 |
회원관리삭제 (0) | 2022.05.25 |
관리자 계정 시스템 (0) | 2022.05.25 |