Search Module

From JCFWiKi

Jump to: navigation, search

그림:check.gif

  • 산출물 : JCF 검색기능 구현하기
  • 작성자:송희정
  • 최초작성일 : 2008/05/20
  • 최종작성일 : 2008/05/20

Copyright © 2008 Daewoo Information Systems Co., Ltd.

그림:forbidden.gif

이 곳은 검색기능을 구현하기 위한 설명이 있다.

목차

[편집] 개요

  • 기존 목록보기에서 추가로 검색기능을 구현하는 것으로, 키워드를 입력하여 검색된 데이터들을 목록으로 나열하는 형태이다.

[편집] 소스다운받기

그림:information.gif

[편집] 코딩할 부분

  • JSP
    • 검색어 입력폼 추가
    • 검색대상과 검색단어를 파라미터로 넘겨야함
  • Action
    • 액션 클래스의 조회 메소드 변경(검색 파라미터를 받아 서비스 클래스의 메소드로 전달)
    • 검색 조건을 위한 액션 프라퍼티 추가
  • Service
    • DAO 클래스의 조회 메소드의 파라미터가 검색 조건을 전달 받을 수 있도록 변경
  • DAO
    • DAO 클래스에서는 SQL에 검색 조건을 전달하도록 변경
  • Statement XML
    • SELECT에 where 구문을 추가
    • Dynamic Statement 사용

[편집] 구현하기

[편집] Statement XML

  • select 구문에서 Key로 받은 검색 값을 확인하여 where 구문을 만든다.
  • Dynamic Query 적용
	<statement id="findUsers" resultMap="user-resultMap" parameterClass="java.util.Map">
		SELECT  id, name, address_code_id
		FROM USERS
		<dynamic prepend="WHERE">
		<isNotEmpty property="searchKey">
			$searchKey$ like '%$searchValue$%'
		</isNotEmpty>
		</dynamic>
	</statement>

만약에 검색어 컬럼에 따라 like 구문이나 equal 구문을 구분해야 하는 경우는 다음과 같이 사용한다.

	<statement id="findUsers" resultMap="user-resultMap" parameterClass="java.util.Map">
		SELECT  id, name, address_code_id
		FROM USERS
		<dynamic prepend="WHERE">
		<isEqual property="searchKey" compareValue="id">
		     $searchKey$ = #searchValue#
		</isEqual>
		<isEqual property="searchKey" compareValue="name">
		     $searchKey$ like '%$searchValue$%
		</isEqual>
		</dynamic>
	</statement>

[편집] Action

  • 검색 조건을 위한 프라퍼티 선언(getter/setter 추가)
  • findUsers(..) 메소드에서는 서비스 클래스로 검색 조건을 해시맵 객체로 구성하여 넘겨줌
public class UserAction extends BaseAction {
	/*
	 * ******************************** PROPERTIES
	 * ******************************
	 */private int page;
	
	private String searchKey;
	private String searchValue;
 
 
   // searchKey, searchValue의 getter/setter 메소드 추가
 
public String findUsers() {
	if (this.page < 1) {
		page = 1;
	}
	int pageSize = 5;
	
	HashMap searchCondition = new HashMap();
	searchCondition.put("searchKey", searchKey);
	searchCondition.put("searchValue", searchValue);
	
	userList = userService.findUsers(searchCondition, page, pageSize);
	..
	return SUCCESS;
}

[편집] JSP

  • 사용자 조회화면
<s:form action="findUsers" theme='simple'>
<table cellpadding="3" cellspacing="0" border="0" width="500">
	<tr>
		<td width="70"><s:select name="searchKey" 
                    list="#{'name':'이름', 'id':'ID'}"/></td>
		<td width="130"><s:textfield name="searchValue"/></td>
		<td width="300"><s:submit value="조회"/></td>
	</tr>
</table>
</s:form>
 
<table cellpadding="3" cellspacing="0" border="1" width="500">
    <tr>
        <td align="center">아이디</td>
        <td align="center">이름</td>
        <td align="center">암호</td>