@3/20/2023
PostSearchDTO
•
검색 정보를 posetSearchDTO로 전달
@Getter @Setter
public class PostSearchDTO {
private String query;
private String queryType;
}
Java
복사
controller
•
@ModelAttribute를 통해 posetSearchDTO를 전달받음
@GetMapping
public String postList(
@ModelAttribute PostSearchDTO postSearchDTO,
@RequestParam(required = false) Integer currentPage,
@CookieValue(required = false) Integer pageSize,
Model model,
HttpServletResponse response
) {
if (currentPage == null || currentPage < 1) {
currentPage = 1;
}
if (pageSize == null || (pageSize !=10 && pageSize != 30 && pageSize != 50)) {
response.addCookie(new Cookie("pageSize", "10"));
pageSize = 10;
currentPage = 1;
}
List<PostListDTO> postListDTOs = postService.findAll(currentPage, pageSize, postSearchDTO);
model.addAttribute("postListDTOs", postListDTOs);
PostPagerDTO postPagerDTO = postService.getPager(currentPage, pageSize);
model.addAttribute("postPagerDTO", postPagerDTO);
return "posts/postList";
}
Java
복사
service
public List<PostListDTO> findAll(int pageNum, int pageSize, PostSearchDTO postSearchDTO) {
...
List<PostListDTO> postListDTOs = postRepository.findAll(firstIndex, pageSize, postSearchDTO);
...
}
Java
복사
repository
•
jpql로 동적 쿼리를 생성하느라 매우 지저분해짐. 추후 QueryDSL로 리팩터링 예정
public List<PostListDTO> findAll(int offset, int limit, PostSearchDTO postSearchDTO) {
String jpql = "select new dev.gyuray.forum.repository.post.PostListDTO(" +
"p.id, " +
"p.title, " +
"u.name, " +
"p.regDate, " +
"p.view, " +
"''" +
") " +
"from Post p " +
"join p.user u ";
String searchQuery = "";
String searchQueryType = "";
boolean isSearching = false;
if (postSearchDTO != null && postSearchDTO.getQueryType() != null &&postSearchDTO.getQuery() != null) {
searchQueryType = postSearchDTO.getQueryType(); //p.title, p.content, u.name
searchQuery = postSearchDTO.getQuery();
if (searchQueryType.equals("user")) {
jpql += "where u.name like :query ";
} else {
jpql += "where p." + searchQueryType + " like :query ";
}
isSearching = true;
}
jpql += "order by p.id desc";
TypedQuery<PostListDTO> query = em.createQuery(jpql, PostListDTO.class);
if (isSearching) {
query.setParameter("query", "%" + searchQuery + "%");
}
return query
.setFirstResult(offset)
.setMaxResults(limit)
.getResultList();
}
Java
복사
view
•
enum 사용 시 괜히 복잡해질 것 같아 단순하게 구현함
•
controller에서 @ModelAttribute로 받고 자동으로 다시 모델로 넘어오기 때문에 검색 내용 보존 가능
<!-- search bar -->
<form class="row d-flex justify-content-center align-items-center" action="/posts" method="get">
<div class="col-md-5">
<div class="row">
<div class="col-md-4 p-0">
<select class="form-select" name="queryType">
<option value="title" th:selected="${postSearchDTO.query == 'title'}">제목</option>
<option value="content" th:selected="${postSearchDTO.query == 'content'}">내용</option>
<option value="user" th:selected="${postSearchDTO.query == 'user'}">작성자</option>
</select>
</div>
<div class="col-md-8 p-0">
<input th:value="${postSearchDTO.query}" name="query" class="form-control" type="text" placeholder="검색 내용" aria-label="Search">
</div>
</div>
</div>
<div class="col-md-1 p-0">
<button class="btn btn-outline-secondary" type="submit">
<i class="bi bi-search"></i>
</button>
</div>
</form>
HTML
복사