VO ( 검색 옵션, 검색 내용 )

	private String searchGubun;
	private String searchText;
    
    	public String getSearchGubun() {
		return searchGubun;
	}
	public void setSearchGubun(String searchGubun) {
		this.searchGubun = searchGubun;
	}
	public String getSearchText() {
		return searchText;
	}
	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

 

 

SQL.xml ( List )

isNotNull로 검색이 안됫을 때도 페이지가 출력되게 해야 됨 

	<select id="boardDAO.selectNBoardList" resultClass="egovMap">
	
	
				select b.* from (
        select row_number() over (order by unq desc) as rn, a.* from (
        
		 select	unq ,title, name, hits, DATE_FORMAT(rdate, '%Y-%m-%d')as rdate
		 from nboard
		 
		 	<isNotNull property="searchGubun">
		 		<isNotNull property="searchText">
		 	where $searchGubun$ like '%$searchText$%'
		 		</isNotNull>
			</isNotNull>
			
		 order by unq desc ) as a ) as b
		 
         <![CDATA[
         where rn >= #startIndex# and rn <= #endIndex#
         ]]>
         
	</select>

 

 

SQL.xml ( count )

총 게시물 갯수도 검색이 됫을 때 검색 된 내용으로 나와야 되기 때문에 위에 내용 그대로 복사 붙혀 넣기

	<select id="boardDAO.selectNBoardTotal" resultClass="java.lang.Integer">
		
		select count(*) total from nboard
			<isNotNull property="searchGubun">
		 		<isNotNull property="searchText">
		 		where $searchGubun$ like '%$searchText$%'
		 		</isNotNull>
			</isNotNull>
		
		
	</select>

 

JSP

검색

사용자가 입력한 옵션을 기억하고, 페이지가 다시 로드될 때 그옵션이 선택된 상태로 유지되게 하려고

${param.searchGubun == 'title' ? 'selected' : ''}

"${param.searchText}"

사용 

    <div class="div2">
        <form name="searchFrm" method="post" action="boardList.do">
        <select name="searchGubun" id="searchGubun">
            <option value="title" ${param.searchGubun == 'title' ? 'selected' : ''}>제목</option>
            <option value="name" ${param.searchGubun == 'name' ? 'selected' : ''}>글쓴이</option>
            <option value="content" ${param.searchGubun == 'content' ? 'selected' : ''}>내용</option>
        </select>
        <input type="text" name="searchText" id="searchText" value="${param.searchText}"/>
        <button type="submit">검색</button>
        </form>
    </div>

 

 

페이징

검색 조건이 있는 경우와 없는 경우를 <c:choose>, <c:when>, <c:otherwise>를 사용하여 처리

검색 조건이 있을 때 : 검색 조건을 포함하는 페이지 링크 생성 방법 ( 검색 조건 유지 ) 

검색 조건이 없을 때 : 기본 페이지 링크 생성 방법 

 

사용자가 선택한 검색 범주(예: 제목, 내용 등)를 URL 매개변수로 추가
${param.searchGubun}은 사용자가 이전에 선택한 검색 범주를 가지고 있음

사용자가 입력한 검색어를 URL 매개변수로 추가
${fn:escapeXml(param.searchText)}는 사용자가 입력한 검색어를 가져옴

fn:escapeXml 함수를 사용해 이 입력값에서 XML/HTML 태그를 이스케이프하여 보안을 강화
즉, 스크립팅 공격으로부터 보호
사용하려면 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 태그를 추가 해야됨

<div style="width:600px; margin-top:5px; text-align:center;">
    <c:forEach var="i" begin="1" end="${totalPage}">
        <c:choose>
            <c:when test="${not empty param.searchGubun and not empty param.searchText}">
                <!-- 검색 조건이 있는 경우 -->
                <a href="boardList.do?viewPage=${i}&searchGubun=${param.searchGubun}&searchText=${fn:escapeXml(param.searchText)}"> ${i }</a>
            </c:when>
            <c:otherwise>
                <!-- 검색 조건이 없는 경우 -->
                <a href="boardList.do?viewPage=${i}"> ${i }</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</div>

 

'전자정부프레임워크' 카테고리의 다른 글

CSV파일_MySQL_우편번호  (1) 2024.01.15
상세보기 + 조회수  (0) 2024.01.06
행 번호 관련  (0) 2024.01.06
오라클, MySQL 페이징 쿼리문 + iBATIS  (0) 2024.01.05
log4j2.xml  (2) 2024.01.04

+ Recent posts