listPage.jsp 복사해서 listPageSearch.jsp생성
주소 수정
<c:if test="${page.prev}">
<span>[ <a href="/board/listPageSearch?num=${page.startPageNum - 1}">이전</a>
]
</span>
</c:if>
<c:forEach begin="${page.startPageNum}" end="${page.endPageNum}" var="num">
<span> <c:if test="${select != num}">
<a href="/board/listPageSearch?num=${num}">${num}</a>
</c:if> <c:if test="${select == num}">
<b>${num}</b>
</c:if>
</span>
</c:forEach>
<c:if test="${page.next}">
<span>[ <a href="/board/listPageSearch?num=${page.endPageNum + 1}">다음</a>
]
</span>
</c:if>
BoardController 에 추가
// 게시물 목록 + 페이징 + 검색
@GetMapping("/listPageSearch")
public void getlistPageSearch(@RequestParam("num") int num, Model model) throws Exception {
int count = boardService.count();
Page page = new Page();
page.setNum(num);
page.setCount(count);
page.dataCalc();//페이지와 총게시글로 계산하기
List<BoardVO> list = boardService.listPage(page.getDisplayPost(), page.getPostNum());
model.addAttribute("list", list); //현재 num 페이지의 게시글 데이터
model.addAttribute("page", page); // 페이지 객체
model.addAttribute("select", num);//현재 선택한 페이지
}
home.jsp - 링크 추가
<p><a href="/board/listPageSearch?num=1">글 목록(페이징+검색)</a></p>
listPageSearch.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 prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시물 페이징 + 검색</title>
</head>
<body>
<div id="nav">
<%@ include file="../include/nav.jsp"%>
</div>
<table>
<thead>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성일</th>
<th>작성자</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="board">
<tr>
<td>${board.bno}</td>
<td><a href="/board/view?bno=${board.bno}">${board.title}</a></td>
<td><fmt:formatDate value="${board.regDate}"
pattern="yyyy-MM-dd" /></td>
<td>${board.writer}</td>
<td>${board.viewCnt}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<c:if test="${page.prev}">
<span>[ <a
href="/board/listPageSearch?num=${page.startPageNum - 1}">이전</a> ]
</span>
</c:if>
<c:forEach begin="${page.startPageNum}" end="${page.endPageNum}"
var="num">
<span> <c:if test="${select != num}">
<a href="/board/listPageSearch?num=${num}">${num}</a>
</c:if> <c:if test="${select == num}">
<b>${num}</b>
</c:if>
</span>
</c:forEach>
<c:if test="${page.next}">
<span>[ <a
href="/board/listPageSearch?num=${page.endPageNum + 1}">다음</a> ]
</span>
</c:if>
<!-- 검색 -->
<form method="get">
<select name="searchType">
<option value="title">제목</option>
<option value="content">내용</option>
<option value="title_content">제목+내용</option>
<option value="writer">작성자</option>
</select>
<input type="text" name="keyword" />
<button type="submit">검색</button>
</form>
</div>
</body>
</html>
쿼리문 작성 필요
boardMapper.xml에 추가
<!-- 게시글 목록 + 페이징+검색 -->
<select id="listPageSearch" parameterType="hashMap"
resultType="com.board.domain.BoardVO">
SELECT bno, title, content, writer, regDate, viewCnt
FROM tbl_board
<if test="searchType.equals('title')">
WHERE title LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('content')">
WHERE content LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('writer')">
WHERE writer LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('title_content')">
WHERE title LIKE CONCAT('%',#{keyword},'%')
or content LIKE CONCAT('%',#{keyword},'%')
</if>
ORDER BY bno desc
limit #{displayPost}, #{postNum};
</select>
BoardDAO에 추가
//게시글 목록 + 페이징 +검색
public List<BoardVO> listPageSearch(int displayPost, int postNum, String searchType, String keyword) throws Exception;
BoardDAOImple 추가
//게시글 목록 + 페이징 + 검색
@Override
public List<BoardVO> listPageSearch(int displayPost, int postNum, String searchType, String keyword)
throws Exception {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("displayPost", displayPost);
data.put("postNum", postNum);
data.put("searchType", searchType);
data.put("keyword", keyword);
return sqlTemplate.selectList("board.listPageSearch",data );
}
BoardService
// 게시물 목록 + 페이징 + 검색
public List<BoardVO> listPageSearch(
int displayPost, int postNum, String searchType, String keyword) throws Exception;
BoardServiceImple
//게시글 목록 + 페이징 + 검색
@Override
public List<BoardVO> listPageSearch(int displayPost, int postNum, String searchType, String keyword)
throws Exception {
return boardDAO.listPageSearch(displayPost, postNum, searchType, keyword);
}
BoardController
// 게시물 목록 + 페이징 + 검색
@GetMapping("/listPageSearch")
public void getlistPageSearch(@RequestParam(value = "num", required = false, defaultValue="1") int num, Model model,
@RequestParam(value = "searchType", required = false, defaultValue="title") String searchType,
@RequestParam(value = "keyword", required = false, defaultValue="") String keyword
) throws Exception {
int count = boardService.count();
Page page = new Page();
page.setNum(num);
page.setCount(count);
page.dataCalc();//페이지와 총게시글로 계산하기
List<BoardVO> list = boardService.listPageSearch(page.getDisplayPost(), page.getPostNum(),searchType,keyword);
model.addAttribute("list", list); //현재 num 페이지의 게시글 데이터
model.addAttribute("page", page); // 페이지 객체
model.addAttribute("select", num);//현재 선택한 페이지
}
nav.jsp에
글 목록(페이징 + 검색)추가
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<ul>
<li>
<a href="/board/listPageSearch?num=1">글 목록(페이징 + 검색)</a>
</li>
<li>
<a href="/board/list">글 목록</a>
</li>
<li>
<a href="/board/write">글 작성</a>
</li>
<li>
<a href="/board/listPage?num=1">글 목록(페이징)</a>
</li>
</ul>
다른 페이지로 넘어가면
searchType이랑 keyword가 유지되지 않음
boardMapper.xml
<!-- 검색 결과 총 갯수 -->
<select id="searchCount" parameterType="hashMap"
resultType="int">
SELECT COUNT(bno)
FROM
tbl_board
<if test="searchType.equals('title')">
WHERE title LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('content')">
WHERE content LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('writer')">
WHERE writer LIKE CONCAT('%',#{keyword},'%')
</if>
<if test="searchType.equals('title_content')">
WHERE title LIKE CONCAT('%',#{keyword},'%')
or content LIKE
CONCAT('%',#{keyword},'%')
</if>
</select>
BoardDAO
// 게시물 총 갯수 + 검색 적용
public int searchCount(String searchType, String keyword) throws Exception;
BoardDAOImple
//게시물 총 갯수 + 검색 적용
@Override
public int searchCount(String searchType, String keyword) throws Exception {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("searchType", searchType);
data.put("keyword", keyword);
return sqlTemplate.selectOne("board.searchCount", data);
}
BoardService
// 게시물 총 갯수 + 검색 적용
public int searchCount(String searchType, String keyword) throws Exception;
BoardServiceImple
@Override
public int searchCount(String searchType, String keyword) throws Exception {
return boardDAO.searchCount(searchType, keyword);
}
BoardController
// 게시물 목록 + 페이징 + 검색
@GetMapping("/listPageSearch")
public void getlistPageSearch(@RequestParam(value = "num", required = false, defaultValue="1") int num, Model model,
@RequestParam(value = "searchType", required = false, defaultValue="title") String searchType,
@RequestParam(value = "keyword", required = false, defaultValue="") String keyword
) throws Exception {
int count = boardService.searchCount(searchType, keyword);// 게시글 총 갯수
Page page = new Page();
page.setNum(num);
page.setCount(count);
page.dataCalc();//페이지와 총게시글로 계산하기
List<BoardVO> list = boardService.listPageSearch(page.getDisplayPost(), page.getPostNum(),searchType,keyword);
model.addAttribute("list", list); //현재 num 페이지의 게시글 데이터
model.addAttribute("page", page); // 페이지 객체
model.addAttribute("select", num);//현재 선택한 페이지
model.addAttribute("searchType", searchType);
model.addAttribute("keyword",keyword);
}
llistPageSearch.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 prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시물 페이징 + 검색</title>
</head>
<body>
<div id="nav">
<%@ include file="../include/nav.jsp"%>
</div>
<table>
<thead>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성일</th>
<th>작성자</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="board">
<tr>
<td>${board.bno}</td>
<td><a href="/board/view?bno=${board.bno}">${board.title}</a></td>
<td><fmt:formatDate value="${board.regDate}"
pattern="yyyy-MM-dd" /></td>
<td>${board.writer}</td>
<td>${board.viewCnt}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<c:if test="${page.prev}">
<span>[ <a
href="/board/listPageSearch?num=${page.startPageNum - 1}&searchType=${searchType}&keyword=${keyword}">이전</a> ]
</span>
</c:if>
<c:forEach begin="${page.startPageNum}" end="${page.endPageNum}"
var="num">
<span> <c:if test="${select != num}">
<a href="/board/listPageSearch?num=${num}&searchType=${searchType}&keyword=${keyword}">${num}</a>
</c:if> <c:if test="${select == num}">
<b>${num}</b>
</c:if>
</span>
</c:forEach>
<c:if test="${page.next}">
<span>[ <a
href="/board/listPageSearch?num=${page.endPageNum + 1}&searchType=${searchType}&keyword=${keyword}">다음</a> ]
</span>
</c:if>
<%-- <c:forEach begin="1" end="${pageNum}" var="num">
<span>
<a href="/board/listPage?num=${num}">${num}</a>
</span>
</c:forEach> --%>
<!-- 검색 -->
<form method="get">
<select name="searchType">
<option value="title"
<c:if test="${searchType eq 'title'}">selected</c:if>>제목</option>
<option value="content"
<c:if test="${searchType eq 'content'}">selected</c:if>>내용</option>
<option value="title_content"
<c:if test="${searchType eq 'title_content'}">selected</c:if>>제목+내용</option>
<option value="writer"
<c:if test="${searchType eq 'writer'}">selected</c:if>>작성자</option>
</select>
<input type="text" name="keyword" value="${keyword}"/>
<button type="submit">검색</button>
</form>
</div>
</body>
</html>
'BACKEND > Spring' 카테고리의 다른 글
게시판 만들기 - 댓글 작성(write) (0) | 2023.10.25 |
---|---|
게시판 만들기 - 댓글 DB에 테이블 추가, 게시글 댓글 조회 (0) | 2023.10.25 |
게시판 만들기 - 페이징 기능 구현2 (0) | 2023.10.24 |
게시판 만들기 - 페이징 기능 구현 (1) | 2023.10.24 |
게시판 만들기 - 조회수 증가(UPDATE) 기능 추가 (0) | 2023.10.24 |