main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="my-4">게시판</h1>
<a href="write.do" class="btn btn-primary mb-3">새 글 작성</a>
<a href="logout.do" class="btn btn-primary mb-3">로그아웃</a>
<form action="main.do" method="GET">
<p>
<select name="board_search_key" id="key">
<option value="name">이름</option>
<option value="context">내용</option>
<option value="title">제목</option>
</select>
<input type="text" name="board_search_value" id="context" class="form-floating mb-4" property="검색어를 입력해주세요"/>
</p>
</form>
<table class="table table-bordered">
<tbody>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
<th>상세보기</th>
</tr>
<c:if test="${not empty datas}">
<c:forEach var="board" items="${datas}">
<c:set var="titleAndContent" value="${board.board_title}" />
<c:if test="${param.board_search_key == 'content'}">
<c:set var="titleAndContent" value="${board.board_content}" />
</c:if>
<tr>
<td>${board.board_num}</td>
<td class="titleAndContent">${titleAndContent}</td>
<td>${board.board_writer}</td>
<td>${board.board_date}</td>
<td>
<a href="view.do?board_num=${board.board_num}" class="btn btn-info mb-2">보기</a>
</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${empty datas}">
<tr>
<td colspan="5" class="text-center">게시글이 없습니다.</td>
</tr>
</c:if>
</tbody>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function(){
var maxLength = 20;
$('titleAndContent').each(function(){
let titleAndContent = $(this).text();
if(titleAndContent.length > maxLength){
titleAndContent = titleAndContent.substring(0, maxLength) + '...';
}
$(this).text(titleAndContent);
})
})
</script>
</body>
</html>
BoardViewPageController.java
package com.nsd.app.view.board;
import com.nsd.app.biz.board.BoardDAO;
import com.nsd.app.biz.board.BoardDTO;
import com.nsd.app.biz.board.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class BoardViewPageController {
@Autowired
BoardService boardService;
@GetMapping("/main.do")
public String mainPage(Model model, BoardDTO boardDTO) throws Exception {
List<BoardDTO> datas = boardService.SelectAll(boardDTO);
model.addAttribute("datas", datas);
return "main";
}
@GetMapping("/view.do")
public String BoardView(BoardDAO boardDAO, BoardDTO boardDTO, Model model) throws Exception {
boardDTO = boardDAO.SelectOne(boardDTO);
model.addAttribute("data", boardDTO);
return "view";
}
}
BoardDTO.java
package com.nsd.app.biz.board;
public class BoardDTO {
private int board_num;
private String board_title;
private String board_context;
private String board_writer;
private String board_date;
private String board_name;
private String board_search_key;
private String board_search_value;
private String board_condition;
public String getBoard_name() {
return board_name;
}
public void setBoard_name(String board_name) {
this.board_name = board_name;
}
public String getBoard_condition() {
return board_condition;
}
public void setBoard_condition(String board_condition) {
this.board_condition = board_condition;
}
public int getBoard_num() {
return board_num;
}
public void setBoard_num(int board_num) {
this.board_num = board_num;
}
public String getBoard_title() {
return board_title;
}
public void setBoard_title(String board_title) {
this.board_title = board_title;
}
public String getBoard_context() {
return board_context;
}
public void setBoard_context(String board_context) {
this.board_context = board_context;
}
public String getBoard_writer() {
return board_writer;
}
public void setBoard_writer(String board_writer) {
this.board_writer = board_writer;
}
public String getBoard_date() {
return board_date;
}
public void setBoard_date(String board_date) {
this.board_date = board_date;
}
public String getBoard_search_key() {
return board_search_key;
}
public void setBoard_search_key(String board_search_key) {
this.board_search_key = board_search_key;
}
public String getBoard_search_value() {
return board_search_value;
}
public void setBoard_search_value(String board_search_value) {
this.board_search_value = board_search_value;
}
@Override
public String toString() {
return "BoardDTO{" +
"board_num=" + board_num +
", board_title='" + board_title + '\'' +
", board_context='" + board_context + '\'' +
", board_writer='" + board_writer + '\'' +
", board_date='" + board_date + '\'' +
", board_name='" + board_name + '\'' +
", board_search_key='" + board_search_key + '\'' +
", board_search_value='" + board_search_value + '\'' +
", board_condition='" + board_condition + '\'' +
'}';
}
}
BoardDAO.java
package com.nsd.app.biz.board;
import com.nsd.app.biz.common.JDBCUtil;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class BoardDAO{
private final String SQL_ALL = "SELECT BOARD_NUM, BOARD_TITLE, BOARD_CONTEXT, BOARD_DATE, BOARD_WRITER, MEMBER_NAME FROM BOARD B JOIN MEMBER M ON B.BOARD_WRITER = M.MEMBER_ID ORDER BY BOARD_NUM DESC";
private final String SQL_ALL_SEARCH = "SELECT BOARD_NUM, BOARD_TITLE, BOARD_CONTEXT, BOARD_DATE, BOARD_WRITER, MEMBER_NAME FROM BOARD B JOIN MEMBER M ON B.BOARD_WRITER = M.MEMBER_ID ";
private final String SQL_ONE = "SELECT BOARD_NUM, BOARD_TITLE, BOARD_CONTEXT, BOARD_DATE, BOARD_WRITER FROM BOARD WHERE BOARD_NUM = ?";
private final String SQL_INSERT = "INSERT INTO BOARD (BOARD_TITLE, BOARD_CONTEXT,BOARD_WRITER) VALUES (?,?,?)";
private final String SQL_UPDATE = "";
private final String SQL_DELETE = "";
public List<BoardDTO> SelectAll(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
List<BoardDTO> datas = null;
try {
System.out.println("BoardDAO.java log boardDTO.getBoard_search_value()["+boardDTO.getBoard_search_value()+"]");
System.out.println("BoardDAO.java log boardDTO["+boardDTO+"]");
if(boardDTO.getBoard_search_value()==null){
System.out.println("BoardDAO.java log if boardDTO.getBoard_search_value()==null");
pstmt=conn.prepareStatement(SQL_ALL);
}
else if (boardDTO.getBoard_search_value().equals("")) {
System.out.println("BoardDAO.java log else if boardDTO.getBoard_search_value().equals()");
return datas;
}
else{
System.out.println("BoardDAO.java log else");
pstmt=conn.prepareStatement(SQL_ALL_SEARCH + keyword_map(boardDTO.getBoard_search_key()));
// Sql 오류를 피하기 위해 ' 작은 따옴표를 추가합니다.
// strip() 유니코드 모든 공백을 제거하기 위해 추가합니다.
pstmt.setString(1, boardDTO.getBoard_search_value().replace("'", "\'").strip());
}
ResultSet rs=pstmt.executeQuery();
datas = new ArrayList<>();
System.out.println("BoardDAO.java log rs=["+rs+"]");
System.out.println("BoardDAO.java log datas=["+datas+"]");
while(rs.next()) {
BoardDTO board = new BoardDTO();
board.setBoard_num(rs.getInt("BOARD_NUM"));
board.setBoard_title(rs.getString("BOARD_TITLE"));
board.setBoard_context(rs.getString("BOARD_CONTEXT"));
board.setBoard_date(rs.getString("BOARD_DATE"));
board.setBoard_writer(rs.getString("BOARD_WRITER"));
board.setBoard_name(rs.getString("MEMBER_NAME"));
System.out.println("BoardDAO.java log while(rs.next()) {} board=["+board+"]");
datas.add(board);
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return datas;
}
public BoardDTO SelectOne(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
BoardDTO board = null;
try {
pstmt=conn.prepareStatement(SQL_ONE);
pstmt.setInt(1, boardDTO.getBoard_num());
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
board = new BoardDTO();
board.setBoard_num(rs.getInt("BOARD_NUM"));
board.setBoard_title(rs.getString("BOARD_TITLE"));
board.setBoard_context(rs.getString("BOARD_CONTEXT"));
board.setBoard_date(rs.getString("BOARD_DATE"));
board.setBoard_writer(rs.getString("BOARD_WRITER"));
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return board;
}
public boolean insert(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(SQL_INSERT);
pstmt.setString(1, boardDTO.getBoard_title());
pstmt.setString(2, boardDTO.getBoard_context());
pstmt.setString(3, boardDTO.getBoard_writer());
int update=pstmt.executeUpdate();
flag = true;
if(update <= 0) {
System.out.println("업데이트 실패");
return flag;
}
} catch (SQLException e) {
System.out.println("Insert SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return flag;
}
public boolean update(BoardDTO boardDTO) {
return false;
}
public boolean delete(BoardDTO boardDTO) {
return false;
}
public static String keyword_map(String keyword) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", " WHERE MEMBER_NAME LIKE CONCAT('%',?,'%')");
map.put("title", " WHERE BOARD_TITLE LIKE CONCAT('%',?,'%')");
map.put("context", " WHERE BOARD_CONTEXT LIKE CONCAT('%',?,'%')");
return map.get(keyword) + " ORDER BY BOARD_NUM DESC";
}
}
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
Spring 비동기 검색 제작 (0) | 2024.10.14 |
---|---|
중간 프로젝트 Spring 이관작업 (1) | 2024.10.09 |
Spring 기본 @(어노테이션) 활용 (0) | 2024.10.08 |
[Spring] Page 작성 과제(BOARD 관련) (0) | 2024.10.05 |
[Spring] Page 작성 과제(MEMBER 관련) (0) | 2024.10.05 |