authorManage.jsp
)<form>
태그가 있지만 검색 기능에 사용 될 <form>
태그를 새로 추가하여 구현한다.<form>
태그를 활용하여 검색 기능을 구현해도 상관은 없다.<input>
태그에 검색어를 입력 후 enter를 누르면 검색이 되는데 javascript를 통해 기존 <form>
태그 활용하는 방식은 enter를 눌렀을 때 동작하지 않는 단점이 있다.<input>
태그와 검색을 실행하는 <button>
태그를 새로운 <form>
태그 안에 작성하고자 한다.<!-- 검색 영역 -->
<div class="search_wrap">
<form id="searchForm" action="/admin/authorManage" method="get">
<div class="search_input">
<input type="text" name="keyword" value='<c:out value="${pageMaker.cri.keyword}"></c:out>'>
<input type="hidden" name="pageNum" value='<c:out value="${pageMaker.cri.pageNum }"></c:out>'>
<input type="hidden" name="amount" value='${pageMaker.cri.amount}'>
<button class="btn search_btn">검색</button>
</div>
</form>
</div>
<input>
태그를 추가해주었다.<input>
태그는 페이지가 로딩될 때 기본적으로 서버에서 전달받은 pageNum과 amount, keyword값이 저장되도록 설정하였다.let searchForm = $('#searchForm');
/* 작가 검색 버튼 동작 */
$('#searchForm button').on("click", function(e){
e.preventDefault();
/* 검색 키워드 유효성 검사 */
if (!searchForm.find("input[name='keyword']").val()) {
alert("키워드를 입력하세요.");
return false;
}
searchForm.find("input[name='pageNum']").val("1");
searchForm.submit();
});
/* 검색 영역 */
.search_wrap{
margin-top:15px;
}
.search_input{
position: relative;
text-align:center;
}
.search_input input[name='keyword']{
padding: 4px 10px;
font-size: 15px;
height: 20px;
line-height: 20px;
}
.search_btn{
height: 32px;
width: 80px;
font-weight: 600;
font-size: 18px;
line-height: 20px;
position: absolute;
margin-left: 15px;
background-color: #c3daf7;
}
<c:if>
태그 활용하여 작가 목록을 출력하거나 검색결과가 없다는 문구를 출력되도록 할 것이다.서버단계에서 검색 결과 존재 유무를 판단해야한다. 검색결과들은 authorGetList()의 List 타입으로 반환이 되는데 List타입이 가지고 있는 empty() 메서드를 활용하여 검색결과 존재 유무를 판단할 것이다.List클래스의 empty()메서드는 List타입의 데이터가 요소 값을 가지고 있지 않은 경우에 true를 반환, 요소를 하나 이상 가지고 있는 경우 false를 반환한다.
if (!list.isEmpty()) {
model.addAttribute("list", list); //작가 존재
} else {
model.addAttribute("list", "empty"); // 작가 존재하지 않음
}
<div>
태그 코드를 아래의 <c:if>
태그로 감싸준다.<div>
태그가 출력되게 된다.<c:if test="${listCheck != 'empty' }">
<table class="author_table">
<thead>
<tr>
<td class="th_column_1">작가 번호</td>
<td class="th_column_2">작가 이름</td>
<td class="th_column_3">작가 국가</td>
<td class="th_column_4">등록 날짜</td>
<td class="th_column_5">수정 날짜</td>
</tr>
</thead>
<c:forEach items="${list}" var="list">
<tr>
<td><c:out value="${list.authorId}"></c:out> </td>
<td><c:out value="${list.authorName}"></c:out></td>
<td><c:out value="${list.nationName}"></c:out> </td>
<td><fmt:formatDate value="${list.regDate}" pattern="yyyy-MM-dd"/></td>
<td><fmt:formatDate value="${list.updateDate}" pattern="yyyy-MM-dd"/></td>
</tr>
</c:forEach>
</table>
</c:if>
<c:if>
태그 바로 다음 공간에 아래의 <c:if>
태그 코드를 추가해준다. 검색 결과가 없을 경우 출력되게 될 문구다.<!-- 게시물이 없으면 -->
<c:if test="${listCheck == 'empty' }">
<div class="table_empty">
등록된 작가가 없습니다.
</div>
</c:if>
<div>
태그에 css설정을 해주기위해 AuthorManage.css에 아래의 코드를 추가했다..table_empty{
height: 50px;
text-align: center;
margin: 200px 0 215px 0px;
font-size: 25px;
}