list.jsp
<div class="container">
<div class="m-2">
<form class="form-inline d-flex justify-content-end" action="/blog/board">
<input type="hidden" name="cmd" value="search" />
<input type="hidden" name="page" value="0" />
<input type="text" name="keyword" class="form-control mr-sm-2" placeholder="Search">
<button class="btn btn-primary m-1">검색</button>
</form>
</div>
<div class="progress col-md-12 m-2">
<div class="progress-bar" style="width: ${currentPosition}%"></div>
</div>
위의 form 태그를 유심히 보면 action="/blog/board" 이후에
그 밑에 <input> 들의 name, value가 서버로 전송 될 것임을 알 수 있다.
action = "blog/board?cmd=search&page=0&keyword=[검색한단어]"
BoardController.java
else if(cmd.equals("search")) {
String keyword = request.getParameter("keyword");
int page = Integer.parseInt(request.getParameter("page"));
List<Board> boards = boardService.글검색(keyword, page);
request.setAttribute("boards", boards);
int boardCount = boardService.글개수(keyword);
int lastPage = (boardCount-1)/4; // 2/4 = 0, 3/4 = 0, 4/4 = 1, 9/4 = 2 ( 0page, 1page, 2page)
double currentPosition = (double)page/(lastPage)*100;
request.setAttribute("lastPage", lastPage);
request.setAttribute("currentPosition", currentPosition);
RequestDispatcher dis = request.getRequestDispatcher("board/list.jsp");
dis.forward(request, response);
}
cmd.equals("list")와 코드가 유사하다.
글검색() 의 결과가 저장된 boards 를 setAttribute 하여 dispatcher 를 이용해 list페이지로 이동하게 한다.
글검색 된 boards 도 페이지처리가 적용되어야 하기 때문에 글개수(keyword) 함수를 오버로딩하여 새로 만들어야 한다.
BoardService.java
public List<Board> 글검색(String keyword, int page){
return boardDao.findByKeyword(keyword, page);
}
public int 글개수(String keyword) {
return boardDao.count(keyword);
}
BoardDao.java
public List<Board> findByKeyword(String keyword, int page){
String sql = "SELECT * FROM board WHERE title like ? ORDER BY id DESC LIMIT ?, 4"; // 0,4 4,4 8,4
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Board> boards = new ArrayList<>();
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+keyword+"%");
pstmt.setInt(2, page*4); // 0 -> 0, 1 ->4, 2->8
rs = pstmt.executeQuery();
// Persistence API
while(rs.next()) { // 커서를 이동하는 함수
Board board = Board.builder()
.id(rs.getInt("id"))
.title(rs.getString("title"))
.content(rs.getString("content"))
.readCount(rs.getInt("readCount"))
.userId(rs.getInt("userId"))
.createDate(rs.getTimestamp("createDate"))
.build();
boards.add(board);
}
return boards;
} catch (Exception e) {
e.printStackTrace();
} finally { // 무조건 실행
DB.close(conn, pstmt, rs);
}
return null;
}
public int count(String keyword) {
String sql = "SELECT count(*) FROM board WHERE title like ?";
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+keyword+"%");
rs = pstmt.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally { // 무조건 실행
DB.close(conn, pstmt, rs);
}
return -1;
}
BoardDao.list 와 코드가 유사하다.
list.jsp
<!-- disabled -->
<ul class="pagination justify-content-center">
<c:choose>
<c:when test="${empty param.keyword}">
<c:set var ="pagePrev" value="/blog/board?cmd=list&page=${param.page-1 }"></c:set>
<c:set var="pageNext" value="/blog/board?cmd=list&page=${param.page+1}"/>
</c:when>
<c:otherwise>
<c:set var="pagePrev" value="/blog/board?cmd=search&page=${param.page-1}&keyword=${param.keyword}"/>
<c:set var="pageNext" value="/blog/board?cmd=search&page=${param.page+1}&keyword=${param.keyword}"/>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${param.page == 0}">
<li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
</c:when>
<c:otherwise>
<li class="page-item"><a class="page-link" href="${pageScope.pagePrev}">Previous</a></li>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${lastPage == param.page}">
<li class="page-item disabled"><a class="page-link" href="#">Next</a></li>
</c:when>
<c:otherwise>
<li class="page-item"><a class="page-link" href="${pageScope.pageNext}">Next</a></li>
</c:otherwise>
</c:choose>
</ul>
</div>
</body>
</html>
페이지 버튼도 동적으로 바뀌게 한다. 이 부분이 가장 머리를 많이 쓰는 부분이긴 한데...
ㅠㅠ
'국비지원 Spring프레임워크 > JSP dynamic web project blog' 카테고리의 다른 글
blog 13. 댓글 삭제 (0) | 2021.03.03 |
---|---|
blog 12. 댓글 목록 보기 (0) | 2021.03.03 |
blog 11. 댓글 쓰기 ajax (gson 정리) (0) | 2021.03.02 |
blog 10. 글 수정하기 (0) | 2021.02.26 |
blog 9. 글 삭제 ajax, 공통Dto (0) | 2021.02.23 |