12.공지사항 검색
공지사항 검색
순서
1.역할
2.Mapper 처리
3.Service 처리
4.controller 처리
5.view 처리
1.역할
- view
사용자가 작성한 keyword에 대한 데이터와 검색 결과 페이지에 몇개의 결과물들이 표시될것인지(amount), 몇 번쨰 페이지를 볼것인지(pageNum)에 대한 데이터를 서버에 전송한다.
- Server
서버는 전달받은 keyword,amount,pageNum을 사용하여 DB에서 검색 결과 데이터를 가져온 뒤 클라이언트에 전송을 한다.
결과 화면에 페이지 정보를 출력하기 위해 필요로 한 데이터들 또한 같이 생성하여 전송을 한다.
- view
전달 받은 데이터들을 검색 결과 목록을 출력하고 몇페이지에 대한 정보들을 출력한다.
2.Mapper 처리
- NoticeMapper.java
//공지사항목록(페이징 적용)
public List<NoticeVO> getNoticePaging(Criteria cri);
//공지사항 총 갯수
public int getNoticeTotal(Criteria cri);
검색결과에 대한 데이터와 페이징 데이터를 호출한다.
- NoticeMapper.xml
<!-- 목록페이징 -->
<select id="getNoticePaging"
resultType="com.supermm.model.NoticeVO">
select
nno, ntitle, nwriter_id, ncontent, nregdate, updatedate,nhit
from(select /*+INDEX_DESC(anotice nno) */
rownum rnum, nno, ntitle, nwriter_id, ncontent, nregdate, updatedate, nhit
from anotice where
<include refid="criteria"/>
<![CDATA[
rownum <= #{pageNum} * #{amount}]]>
order by nno)
<![CDATA[where rnum > (#{pageNum} -1) * #{amount}]]>
order by nno
</select>
<!--검색조건 -->
<sql id="criteria">
<if test="searchType =='N'.toString()">
ntitle like '%'||#{keyWord}||'%'and
</if>
</sql>
<!--공지사항 갯수 -->
<select id="getNoticeTotal" resultType="int">
select count(*) from ANOTICE
<if test="keyWord != null">
where ntitle like '%'||#{keyWord}||'%'
</if>
</select>
제목을 검색하기 위해서 검색조건을 바꿔준다.
3.Service 처리
- NoticeService.java
public int getNoticeTotal(Criteria cri);
List<NoticeVO> getNoticePaging(Criteria cri);
- NoticeServiceimpl.java
public List<NoticeVO> getNoticePaging(Criteria cri) {
return noticemapper.getNoticePaging(cri);
}
public int getNoticeTotal(Criteria cri) {
return noticemapper.getNoticeTotal(cri);
}
오버라이딩을 한다.
4.controller 처리
@RequestMapping(value = "/notice-list", method = RequestMethod.GET)
public String noticeListForm(Model model, Criteria cri) {
System.out.println("공지사항목록 페이지 페이징..");
model.addAttribute("list", noticeservice.getNoticePaging(cri));
int total = noticeservice.getNoticeTotal(cri);
PageMakeDTO pageMake = new PageMakeDTO(cri, total);
model.addAttribute("pageMake", pageMake);
return "admin/notice/notice-list";
}
url 맵핑을 해준다.
5.view 처리
<form id="searchForm" method="get" action='notice-list'>
<div class="search">
<select name="searchType">
<option
<c:out value="${pageMake.cri.searchType == null ? 'selected':''}"/>>선택</option>
<option value="N"
<c:out value="${pageMake.cri.searchType == 'N' ? 'selected':''}"/>>제목</option>
</select> <input class="board-search" type="text" name="keyWord"
id="keyWord" placeholder="검색어를 입력하세요" value="${cri.keyWord}" />
<button id="btn-search" class="button board-search-button">검색</button>
</div>
</form>
조건 검색을 사용 할 수 있도록 select 태그와 option태그를 추가 해준다.
댓글남기기