jspblog : board keyword search
This commit is contained in:
@@ -90,6 +90,28 @@ public class BoardDao {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int count(String keword) {
|
||||
String sql = "select count(*) from board where title like ?";
|
||||
|
||||
Connection conn = DB.getConnection();
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
if (conn != null) {
|
||||
try {
|
||||
pstmt = conn.prepareStatement(sql);
|
||||
pstmt.setString(1, "%" + keword + "%");
|
||||
rs = pstmt.executeQuery();
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
DB.close(conn, pstmt, rs);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public DetailResDto findById(int id) {
|
||||
String sql = "select b.*, u.username from board b left outer join user u on b.userId = u.id where b.id = ?";
|
||||
Connection conn = DB.getConnection();
|
||||
@@ -182,4 +204,38 @@ public class BoardDao {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Board> findByKeyword(String keyword, int page) {
|
||||
String sql = "select * from board where title like ? order by id desc limit ?, 4";
|
||||
Connection conn = DB.getConnection();
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
List<Board> boards = new ArrayList<>();
|
||||
|
||||
if (conn != null) {
|
||||
try {
|
||||
pstmt = conn.prepareStatement(sql);
|
||||
pstmt.setString(1, "%" + keyword + "%");
|
||||
pstmt.setInt(2, page * 4);
|
||||
rs = pstmt.executeQuery();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ public class BoardService {
|
||||
public int 글개수() {
|
||||
return boardDao.count();
|
||||
}
|
||||
public int 글개수(String keword) {
|
||||
return boardDao.count(keword);
|
||||
}
|
||||
|
||||
public DetailResDto 글상세보기(int id) {
|
||||
int result = boardDao.updateReadCount(id);
|
||||
@@ -40,4 +43,8 @@ public class BoardService {
|
||||
public int 글수정(UpdateReqDto dto) {
|
||||
return boardDao.update(dto);
|
||||
}
|
||||
|
||||
public List<Board> 글검색(String keyword, int page) {
|
||||
return boardDao.findByKeyword(keyword, page);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +130,20 @@ public class BoardController extends HttpServlet {
|
||||
Script.back(response, "글 수정 실패");
|
||||
}
|
||||
}
|
||||
case "search" : {
|
||||
String keyword = request.getParameter("keyword");
|
||||
int page = Integer.parseInt(request.getParameter("page"));
|
||||
|
||||
List<Board> boards = boardService.글검색(keyword, page);
|
||||
int boardCount = boardService.글개수(keyword);
|
||||
int lastPage = (boardCount - 1) / 4;
|
||||
double currentPosition = (double) page / lastPage * 100;
|
||||
request.setAttribute("boards", boards);
|
||||
request.setAttribute("lastPage", lastPage);
|
||||
request.setAttribute("currentPosition", currentPosition);
|
||||
request.getRequestDispatcher("board/list.jsp").forward(request, response);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@
|
||||
<form class="form-inline d-flex justify-content-end" action="${pageContext.request.contextPath}/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">
|
||||
<input type="text" name="keyword" class="form-control mr-sm-2" placeholder="Search" />
|
||||
<button class="btn btn-primary m-1">검색</button>
|
||||
|
||||
</form>
|
||||
@@ -29,12 +28,23 @@
|
||||
</c:forEach>
|
||||
<br />
|
||||
<ul class="pagination justify-content-center">
|
||||
<c:choose>
|
||||
<c:when test="${empty param.keyword}">
|
||||
<c:set var="pagePrev" value="${pageContext.request.contextPath}/board?cmd=list&page=${param.page-1}" />
|
||||
<c:set var="pageNext" value="${pageContext.request.contextPath}/board?cmd=list&page=${param.page+1}" />
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
<c:set var="pagePrev" value="/jspblog/board?cmd=search&page=${param.page-1}&keyword=${param.keyword}" />
|
||||
<c:set var="pageNext" value="/jspblog/board?cmd=search&page=${param.page+1}&keyword=${param.keyword}" />
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
|
||||
<c:choose>
|
||||
<c:when test="${param.page le 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="${pageContext.request.contextPath}/board?cmd=list&page=${param.page-1}">Previous</a></li>
|
||||
<li class="page-item"><a class="page-link" href="${pageScope.pagePrev}">Previous</a></li>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
<c:choose>
|
||||
@@ -42,7 +52,7 @@
|
||||
<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="${pageContext.request.contextPath}/board?cmd=list&page=${param.page+1}">Next</a></li>
|
||||
<li class="page-item"><a class="page-link" href="${pageScope.pageNext}">Next</a></li>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user