📅2024. 02.07 43일차
@RequestMapping("/usr/article/list")
public String showList(HttpServletRequest req, Model model, @RequestParam(defaultValue = "1") int boardId,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "title,body") String searchKeywordTypeCode,
@RequestParam(defaultValue = "") String searchKeyword) {
int articlesCount = articleService.getArticlesCount(boardId, searchKeywordTypeCode, searchKeyword);
}
@Select("""
<script>
SELECT COUNT(*) AS cnt
FROM article AS A
WHERE 1
<if test="boardId != 0">
AND boardId = #{boardId}
</if>
<if test="searchKeyword != ''">
<choose>
<when test="searchKeywordTypeCode == 'title'">
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<when test="searchKeywordTypeCode == 'body'">
AND A.body LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<otherwise>
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
OR A.body LIKE CONCAT('%',#{searchKeyword},'%')
</otherwise>
</choose>
</if>
ORDER BY id DESC
</script>
""")
public int getArticlesCount(int boardId, String searchKeywordTypeCode, String searchKeyword);
(갯수말고 1 row 표시)
ArticleController의 articleService.getForPrintArticles에 searchKeywordTypeCode,searchKeyword 추가해서 넘겨주기
List<Article> articles = articleService.getForPrintArticles(boardId, itemsInAPage, page, searchKeywordTypeCode,
searchKeyword);
@Select("""
<script>
SELECT A.*, M.nickname AS extra__writer
FROM article AS A
INNER JOIN `member` AS M
ON A.memberId = M.id
WHERE 1
<if test="boardId != 0">
AND A.boardId = #{boardId}
</if>
<if test="searchKeyword != ''">
<choose>
<when test="searchKeywordTypeCode == 'title'">
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<when test="searchKeywordTypeCode == 'body'">
AND A.body LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<otherwise>
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
OR A.body LIKE CONCAT('%',#{searchKeyword},'%')
</otherwise>
</choose>
</if>
ORDER BY A.id DESC
<if test="limitFrom >= 0 ">
LIMIT #{limitFrom}, #{limitTake}
</if>
</script>
""")
searchKeywordTypeCode와 searchKeyword parameter를 받지 못해서이다. 그럼 넣어주면 끝??<div class="pagination flex justify-center mt-3">
<c:set var="paginationLen" value="3" />
<c:set var="startPage" value="${page - paginationLen >= 1 ? page - paginationLen : 1}" />
<c:set var="endPage" value="${page + paginationLen <= pagesCount ? page + paginationLen : pagesCount}" />
<c:if test="${startPage > 1 }">
<a class="btn btn-sm" href="?page=1&boardId=${boardId }&searchKeywordTypeCode=${searchKeywordTypeCode}&searchKeyword=${searchKeyword}">1</a>
<button class="btn btn-sm btn-disabled">...</button>
</c:if>
<c:forEach begin="${startPage }" end="${endPage }" var="i">
<a class="btn btn-sm ${param.page == i ? 'btn-active' : '' }" href="?page=${i }&boardId=${boardId}&searchKeywordTypeCode=${searchKeywordTypeCode}&searchKeyword=${searchKeyword}">${i }</a>
</c:forEach>
<c:if test="${endPage < pagesCount }">
<button class="btn btn-sm btn-disabled">...</button>
<a class="btn btn-sm" href="?page=${pagesCount }&boardId=${boardId }&searchKeywordTypeCode=${searchKeywordTypeCode}&searchKeyword=${searchKeyword}">${pagesCount }</a>
</c:if>
</div>
<div class="pagination flex justify-center mt-3">
<c:set var="paginationLen" value="3" />
<c:set var="startPage" value="${page - paginationLen >= 1 ? page - paginationLen : 1}" />
<c:set var="endPage" value="${page + paginationLen <= pagesCount ? page + paginationLen : pagesCount}" />
<c:set var="baseUri" value="?boardId=${boardId }" />
<c:set var="baseUri" value="${baseUri }&searchKeywordTypeCode=${searchKeywordTypeCode}" />
<c:set var="baseUri" value="${baseUri }&searchKeyword=${searchKeyword}" />
<c:if test="${startPage > 1 }">
<a class="btn btn-sm" href="${baseUri }&page=1">1</a>
<button class="btn btn-sm btn-disabled">...</button>
</c:if>
<c:forEach begin="${startPage }" end="${endPage }" var="i">
<a class="btn btn-sm ${param.page == i ? 'btn-active' : '' }" href="${baseUri }&page=${i }">${i }</a>
</c:forEach>
<c:if test="${endPage < pagesCount }">
<button class="btn btn-sm btn-disabled">...</button>
<a class="btn btn-sm" href="${baseUri }&page=${pagesCount }">${pagesCount }</a>
</c:if>
</div>
<form action="">
<input type="hidden" name="boardId" value="${param.boardId }" />
<select data-value="${param.searchKeywordTypeCode }" class="select select-bordered select-sm w-full max-w-xs"name="searchKeywordTypeCode">
<option value="title">title</option>
<option value="body">body</option>
<option value="title,body">title+body</option>
</select>
<input value="${param.searchKeyword }" name="searchKeyword" type="text" placeholder="What is your searchKeyword?"class="input-sm input input-bordered w-48 max-w-xs" />
<button class="btn btn-ghost btn-sm" type="submit">검색</button>
</form>
jQuery를 사용해 배열을 관리하고자 할 때 each() 메서드를 사용할 수 있다.
each() 메서드는 매개 변수로 받은 것을 사용해 for in 반복문과 같이 배열이나 객체의 요소를 검사할 수 있는 메서드이다.
each() 메서드 두 가지 타입
// jQuery 유틸리티 메서드
$.each(object, function(index, item){
});
// jQuery 일반 메서드
$(selector).each(function(index, item){
})