공부하는 블로그

(웹개발) Search 기능 추가하기 본문

Develop/웹개발

(웹개발) Search 기능 추가하기

모아&모지리 2017. 9. 29. 17:19

JSP파일



VO폴더에 새로운 VO추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.ktds.board.vo;
 
public class BoardSearchVO {
    
    private String searchKeyword;
    private String searchType;
    
    public String getSearchKeyword() {
        return searchKeyword;
    }
    public void setSearchKeyword(String searchKeyword) {
        this.searchKeyword = searchKeyword;
    }
    public String getSearchType() {
        return searchType;
    }
    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }
 
}
cs


BoardDao 추가


BoardDaoImpl 추가


BoardDao SQL 에 parameterType 추가

WHERE쿼리 문에 다이나믹 쿼리문 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        <if test="searchKeyword != null and searchKeyword !=''">
            <if test="searchType ==1"> <!-- 다이나믹쿼리 -->
            AND        B.SUBJECT LIKE '%' || #{searchKeyword} || '%'
            </if>
            <if test="searchType == 2">
            AND        B.CONTENT LIKE '%' || #{searchKeyword} || '%'
            </if>
            <if test="searchType == 3">
            AND        (B.SUBJECT LIKE '%' || #{searchKeyword} || '%'
            OR         B.CONTENT LIKE '%' || #{searchKeyword} || '%')
            </if>
            <if test="searchType == 4">
            </if>    
            <if test="searchType == 5">
            AND        M.NAME LIKE '%' || #{searchKeyword} || '%'
            </if>    
            <if test="searchType == 6">
            AND        M.NICK_NAME LIKE '%' || #{searchKeyword} || '%'
            </if>
            <if test="searchType == 7">
            AND        M.ID LIKE '%' || #{searchKeyword} || '%'
            </if>
cs



BoardService추가


ServiceImpl추가


(코드)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RequestMapping("/board/list")
    public ModelAndView viewListPage(BoardSearchVO boardSearchVO , HttpSession session) {
        
        String searchKeyword = boardSearchVO.getSearchKeyword();
        if ( searchKeyword == null || searchKeyword.length() == 0) {
            boardSearchVO = (BoardSearchVO) session.getAttribute("_SEARCH_");
        }                            //session에서 Keyword를 가져옴 
                                    // Search 후 list를 눌렀을 때 Session 값이 없어지므로!!
        
        List<BoardVO> boardList = boardService.readAllBoard(boardSearchVO);
 
        ModelAndView view = new ModelAndView();
        view.setViewName("board/list");
        view.addObject("boardList", boardList);
        view.addObject("boardSearchVO", boardSearchVO);
        
        session.setAttribute("_SEARCH_", boardSearchVO);  
                                //session의 _SEARCH_ 값에  boardSearchVO 저장
        
        return view;
    }
cs


Cotroller에 추가


(코드)

1
2
3
4
5
6
@RequestMapping("/board/list/init")  // _SEARCH_의 세션값을 초기화
    public String clearSearchSession(HttpSession session) {
        
        session.removeAttribute("_SEARCH_");
        return "redirect:/board/list";
    }
cs



초기화

초기화를 시켜준다.