-
3일차 - 게시물 검색프로젝트 일지/Spring 프로젝트- 헬스 커뮤니티 2023. 2. 22. 21:11
오늘은 다른 팀원 도와주고
얼추 완성한 팀원들의 코드를 내 코드에 합치는 과정에서 디버깅하느라 많은 코드를 작성하지는 못했다
검색 필터는
제목 / 글쓴이의 아이디 / 제목과 글쓴이의 아이디 모두
이렇게 세 가지로 나누었다.
예를 들어 검색창에서 카테고리를 제목으로 하고 b를 입력하면
제목에 b가 들어가는 모든 게시물들을 가져와준다.
나머지 두 가지의 경우도 마찬가지.
postList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>게시물 목록</title> <style> td{ text-align: center; } </style> </head> <body> <h1>헬스 커뮤니티</h1> <br><br> <a href="./logout">로그아웃</a> <a href="./mypage">마이페이지</a> <br><br> <c:if test="${fn:length(postList) == 0}"> <table width=500> <tr> <th>NO.</th> <th>제목</th> <th>글쓴이</th> <th>조회수</th> <th>등록일</th> </tr> <tr> <td colspan="5">게시물이 없습니다.</td> </tr> </table> </c:if> <c:if test="${fn:length(postList) > 0}"> <table width=500> <tr> <th>NO.</th> <th>제목</th> <th>글쓴이</th> <th>조회수</th> <th>등록일</th> </tr> <tr> <td colspan="5"><span id="backMsg"></span></td> </tr> <c:forEach var="post" items="${postList}"> <tr> <td>${post.idx}</td> <td><a href="./detail?postId=${post.postId}">${post.postTitle}</a></td> <td>${post.memberId}</td> <td>${post.readCount}</td> <td><fmt:formatDate value="${post.postDate}" pattern="yyyy-MM-dd"/></td> </tr> </c:forEach> </table> </c:if> <br> <!-- 검색 --> <div class="form-inline"> <form action="./search" method="get"> <select name="searchType" id="searchType"> <option value="title" >제목</option> <option value="writer" >글쓴이</option> <option value="all" >제목/글쓴이</option> </select> <input class="form-control" type="text" id="keyword" name="keyword" value="${keyword}"/> <input type="submit" value="검색" id="search"> </form> </div> <!-- 새 게시물 등록 --> <form action="./regist" method="get"> <br> <input type="submit" value="새 게시물 등록"> </form> <br> <!-- paging --> - <c:if test="${paging.prev}"> <a href='<c:url value="./list?page=${paging.startPage-1}"/>'>이전</a> </c:if> <c:forEach begin="${paging.startPage}" end="${paging.endPage}" var="pageNum"> <a href='<c:url value="/list?page=${pageNum}"/>'>${pageNum}</a> </c:forEach> <c:if test="${paging.next}"> <a href='<c:url value="/list?page=${paging.endPage+1}"/>'>다음</a> </c:if> - </body> </html>
PostController.java
/* 검색 */ @RequestMapping(value = "/search", method = RequestMethod.GET) public ModelAndView search (@RequestParam("searchType") String searchType, @RequestParam("keyword") String keyword) { ModelAndView mav = new ModelAndView(); String url="./post/postList"; List<PostVO> postList = postService.search(searchType, keyword); mav.addObject("postList", postList); mav.setViewName(url); return mav; }
PostService.java
/* 검색 */ public List<PostVO> search(String searchType, String keyword) { List<PostVO> postList = null; try { postList = postDAO.selectCondition(searchType, keyword); } catch (SQLException e) { e.printStackTrace(); } return postList; }
PostDAO.java
/* 검색 */ public List<PostVO> selectCondition(String searchType, String keyword) throws SQLException { con = dataSource.getConnection(); if(searchType.equals("title")) { sql = "select *, row_number() over(order by post_id) as idx from post where post_title like ? order by idx desc"; pstmt = con.prepareStatement(sql); pstmt.setString(1, '%' + keyword + '%'); } else if (searchType.equals("writer")) { sql = "select *, row_number() over(order by post_id) as idx from post where member_id like ? order by idx desc"; pstmt = con.prepareStatement(sql); pstmt.setString(1, '%' + keyword + '%'); } else if (searchType.equals("all")) { sql = "select *, row_number() over(order by post_id) as idx from post where member_id like ? or post_title like ? order by idx desc"; pstmt = con.prepareStatement(sql); pstmt.setString(1, '%' + keyword + '%'); pstmt.setString(2, '%' + keyword + '%'); } rs = pstmt.executeQuery(); List<PostVO> postList = new ArrayList<PostVO>(); while(rs.next()) { PostVO post = new PostVO(rs.getInt(9), rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getString(7), rs.getTimestamp(8)); postList.add(post); } ConnectionManager.closeConnection(rs, pstmt, con); return postList; }
'프로젝트 일지 > Spring 프로젝트- 헬스 커뮤니티' 카테고리의 다른 글
최종 결과물 - <헬스 커뮤니티> (0) 2023.02.27 2일차 - 게시물 목록의 페이징 처리 (0) 2023.02.21 1일차(2) - 게시물 목록 (0) 2023.02.20 1일차(1) - 프로젝트 설계 <헬스 커뮤니티> (0) 2023.02.20