Joshua게시판(SSR)-17-검색

jaegeunsong97·2023년 8월 15일
0

SSR 기반 JoshuaBlog

목록 보기
17/23

깃허브링크

노션링크

  • main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ include file="../layout/header.jsp" %>

<div class="container my-3">
    <div class="d-flex justify-content-end mb-2">
        <form class="d-flex col-lg-3" action="/" method="get">
            <input class="form-control" type="text" placeholder="Search" name="keyword">
            <button class="btn btn-primary">Search</button>
        </form>
    </div>
    <div class="my-board-box row">

        <c:forEach items="${boardPG.content}" var="board">
            <%-- 글 아이템 시작 --%>
            <div class="card col-lg-3 pt-2">
                <img class="card-img-top" style="height: 250px;" src="${board.thumbnail}">
                <hr/>
                <div class="card-body">
                    <div>작성자 : ${board.user.username}</div>
                    <h4 class="card-title my-text-ellipsis">${board.title}</h4>
                    <a href="/board/${board.id}" class="btn btn-primary">상세보기</a>
                </div>
            </div>
            <%-- 글 아이템 끝 --%>
        </c:forEach>
    </div>

    <ul class="pagination mt-3 d-flex justify-content-center">

        <li class="page-item ${boardPG.first ? "disabled" : ""}"><a class="page-link" href="/?page=${boardPG.number -1}&keyword=${param.keyword}">Previous</a></li>
        <li class="page-item ${boardPG.last ? "disabled" : ""}"><a class="page-link" href="/?page=${boardPG.number +1}&keyword=${param.keyword}">Next</a></li>
    </ul>
</div>

<%@ include file="../layout/footer.jsp" %>

검색 그림 추가하기!

여기서 search의 keyword를 컨트롤러로 넘김

  • boardService

public Page<Board> 게시글목록보기(int page, String keyword) {
          if (keyword.isBlank()) {
               return boardQueryRepository.findAll(page);
          } else {
               Page<Board> boardPGPS = boardQueryRepository.findAllByKeyword(page, keyword);
               return boardPGPS;
          }
     }
  • boardQueryRepository
public Page<Board> findAllByKeyword(int page, String keyword) {
          int startPosition = page * SIZE;

          List<Board> boardListPS = em
                    .createQuery("select b from Board b join fetch b.user where b.title like :keyword order by desc")
                    .setParameter("keyword", "%" + keyword + "%")
                    .setFirstResult(startPosition)
                    .setMaxResults(SIZE)
                    .getResultList();

          Long totalCount = em.createQuery("select count(b) from Board b where b.title like :keyword", Long.class)
                    .setParameter("keyword", "%" + keyword + "%")
                    .getSingleResult();


          return new PageImpl<>(boardListPS, PageRequest.of(page, SIZE), totalCount);
     }

주소 집중

검색완료

profile
현재 블로그 : https://jasonsong97.tistory.com/

0개의 댓글