728x90
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>

728x90
'BACKEND > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋๊ธ ์์ฑ(write) (0) | 2023.10.25 |
|---|---|
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋๊ธ DB์ ํ ์ด๋ธ ์ถ๊ฐ, ๊ฒ์๊ธ ๋๊ธ ์กฐํ (0) | 2023.10.25 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ํ์ด์ง ๊ธฐ๋ฅ ๊ตฌํ2 (0) | 2023.10.24 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ํ์ด์ง ๊ธฐ๋ฅ ๊ตฌํ (2) | 2023.10.24 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ์กฐํ์ ์ฆ๊ฐ(UPDATE) ๊ธฐ๋ฅ ์ถ๊ฐ (0) | 2023.10.24 |