1. View
<div class="container mt-3"> <div class="dropdown"> <button type="button" class="btn btn-outline-dark" data-bs-toggle="dropdown"> 기술을 선택하세요 </button> <ul class="dropdown-menu"> <li><a class="dropdown-item" name="keyword" href="?keyword=java">java</a></li> <li><a class="dropdown-item" name="keyword" href="?keyword=javaScript">javasSript</a></li> <li><a class="dropdown-item" name="keyword" href="?keyword=Spring">Spring</a></li> <li><a class="dropdown-item" name="keyword" href="?keyword=HTML">HTML</a></li> <li><a class="dropdown-item" name="keyword" href="?keyword=jQuery">jQuery</a></li> <li><a class="dropdown-item" name="keyword" href="?keyword=MySQL">MySQL</a></li> </ul>
name 을 keyword 로 받는다.
2. 컨트롤러
@GetMapping("/resume/listings") public String listings(HttpServletRequest request, @RequestParam(defaultValue = "1") int page, @RequestParam(value = "keyword", required = false) String keyword) { // 기업 메인 페이지 if (keyword != null) { List<ResumeResponse.ResumeAndUserDTO> responseDTO = resumeRepository.findByResumeAndUser(page, keyword); List<ResumeResponse.ResumeAndUserDTO> resumeList = new ArrayList<>(); for (ResumeResponse.ResumeAndUserDTO dto : responseDTO) { if (dto.isEmployer() == false) { resumeList.add(dto); } } request.setAttribute("resumeList", resumeList); // 페이지네이션 모듈 int totalPage = resumeRepository.countIsEmployerFalse(); ; PagingUtil paginationHelper = new PagingUtil(totalPage, page); request.setAttribute("nextPage", paginationHelper.getNextPage()); request.setAttribute("prevPage", paginationHelper.getPrevPage()); request.setAttribute("first", paginationHelper.isFirst()); request.setAttribute("last", paginationHelper.isLast()); request.setAttribute("numberList", paginationHelper.getNumberList()); return "/resume/listings"; } else { List<ResumeResponse.ResumeAndUserDTO> responseDTO = resumeRepository.findByResumeAndUser(page); List<ResumeResponse.ResumeAndUserDTO> resumeList = new ArrayList<>(); for (ResumeResponse.ResumeAndUserDTO dto : responseDTO) { if (dto.isEmployer() == false) { resumeList.add(dto); } } request.setAttribute("resumeList", resumeList); // 페이지네이션 모듈 int totalPage = resumeRepository.countIsEmployerFalse(); ; PagingUtil paginationHelper = new PagingUtil(totalPage, page); request.setAttribute("nextPage", paginationHelper.getNextPage()); request.setAttribute("prevPage", paginationHelper.getPrevPage()); request.setAttribute("first", paginationHelper.isFirst()); request.setAttribute("last", paginationHelper.isLast()); request.setAttribute("numberList", paginationHelper.getNumberList()); return "/resume/listings"; } }
@RequestParam
어노테이션으로 keyword 값을 받는다. keyword 값이 있으면 레파지토리에 keyword 를 전달한다.3. 레파지토리
public List<ResumeResponse.ResumeAndUserDTO> findByResumeAndUser(int page,String keyword) { final int COUNT = 10; int value = (page - 1) * COUNT; String q = """ SELECT\s r.id, r.user_id, r.title, r.content, r.career,\s u.address, u.is_employer, u.name\s FROM\s resume_tb r\s INNER JOIN\s user_tb u\s ON\s r.user_id = u.id\s INNER JOIN\s skill_tb s\s ON\s r.id = s.resume_id\s WHERE\s u.is_employer = false\s AND s.skill_name = ? ORDER BY r.id DESC LIMIT ?,? """ ; Query query = entityManager.createNativeQuery(q); query.setParameter(1,keyword); query.setParameter(2, value); query.setParameter(3, COUNT); List<Object[]> results = query.getResultList(); List<ResumeResponse.ResumeAndUserDTO> responseDTO = new ArrayList<>(); for (Object[] result : results) { ResumeResponse.ResumeAndUserDTO dto = new ResumeResponse.ResumeAndUserDTO(); dto.setId((Integer) result[0]); dto.setUserId((Integer) result[1]); dto.setTitle((String) result[2]); dto.setContent((String) result[3]); dto.setCareer((String) result[4]); dto.setAddress((String) result[5]); dto.setEmployer((boolean) result[6]); dto.setName((String) result[7]); responseDTO.add(dto); } return responseDTO; } public List<ResumeResponse.ResumeAndUserDTO> findByResumeAndUser(int page) { final int COUNT = 10; int value = (page - 1) * COUNT; String q = """ select r.id,r.user_id,r.title,r.content,r.career,u.address,u.is_employer,u.name from resume_tb r inner join user_tb u on r.user_id = u.id where u.is_employer =false order by id desc limit ?,?; """; Query query = entityManager.createNativeQuery(q); query.setParameter(1, value); query.setParameter(2, COUNT); List<Object[]> results = query.getResultList(); List<ResumeResponse.ResumeAndUserDTO> responseDTO = new ArrayList<>(); for (Object[] result : results) { ResumeResponse.ResumeAndUserDTO dto = new ResumeResponse.ResumeAndUserDTO(); dto.setId((Integer) result[0]); dto.setUserId((Integer) result[1]); dto.setTitle((String) result[2]); dto.setContent((String) result[3]); dto.setCareer((String) result[4]); dto.setAddress((String) result[5]); dto.setEmployer((boolean) result[6]); dto.setName((String) result[7]); responseDTO.add(dto); } return responseDTO; }
오버로딩을 활용해서 keyword 값이 있을 때와 없을 때 두 가지 메서드를 만든다.
4. 화면에 출력
{{#resumeList}} <tr> <td class="div-recommendation-text-2">{{name}}</td> <td> <div class="div-recommendation-text-1">{{career}}</div> <div class="div-recommendation-text-2">{{title}}</div> </td> <td> <button class="btn btn-outline-body" onclick="location.href='/resume/{{id}}'">이력서보기</button> </td> </tr> {{/resumeList}}
쿼리스트링으로 keyword 값을 구분해 데이터를 출력한다.
Share article