JSP Servlet 實現模糊動態查詢並分頁(拼接Sql)

需求:

  • 根據兩個條件查詢。

  • 根據查詢的結果分頁,並記住查詢條件。

大體流程是這樣的:

  • 第一次打開時,默認分頁顯示所有信息,這時候點擊末頁會取到最後一頁數據。

  • 當填寫或選擇了查詢條件時,根據條件取出符合條件的所有數據並分頁。這時候點擊翻頁應該都是這一次查詢條件後的數據。

    我寫的時候思緒挺亂的,來這裏寫一寫,再理一下思路。

  • 建表,只需要一張,還有對應的實體類。

  • 寫前臺展示頁面。

<body>
        <form action="" method="post">
            姓名:<input type="text" name="name"/>
            性別:
             <select name="sex" >
                <option value="-1">全部</option>
                <option value="0"></option>
                <option value="1"></option>         
            </select> 
            <input type="submit" value="搜索"/>
            <table>
                <tr>
                    <td>序號</td>
                    <td>姓名</td>
                    <td>性別</td>
                    <td>頭像</td>
                    <td>職業</td>
                </tr>
            <c:forEach items="" var="">
                ... 
            </c:forEach>
            </table>
        </form>

            <a href="">首頁</a>
            <a href="">上一頁</a>
            當前頁:
            <a href="">下一頁</a>
            <a href="">末頁</a> 

  </body>

開始差不多是這個樣子

  • 剛進入項目要顯示所有,所以可以寫個帶參數的歡迎頁面index.jsp,
    response.sendRedirect("person?action=getMaxPage&name=&sex=0");
    (name=&sex=-1相當於name=空字符串)。把name和sex傳到相應的servlet。

  • 現在來寫servlet

public String getMaxPage(HttpServletRequest req,HttpServletResponse resp){
        resp.setCharacterEncoding("UTF-8");
        /* 剛進入項目時index.jsp傳來name="" 爲空,sex=-1*/
        String name = req.getParameter("name");
        Integer sex =  Integer.valueOf(req.getParameter("sex"));
        /*第一步,通過條件得到最大頁數。要寫一個數據訪問的Dao,虛線內是該Dao裏獲取最大頁的方法*/
        PersonDao pd = new PersonDao();
        Integer maxPage =  pd.getCount(name, sex, rowPerPage);
        //rowPerPage是每頁要顯示的記錄條數,可以定義一個全局變量

--------------------------------------------------------------------------------------
        public Integer getCount(String name,Integer sex,Integer size){
        StringBuffer sb = new StringBuffer("select count(*) from person where 1=1 ");
        ...
        if(!(name.equals(null)) && !(name.equals("")) ){
            sb.append(" and name like '%"+name+"%' ");
        }
        if(sex != 0){
            sb.append(" and sex = "+sex+" ");
        }
        ...
    }
------------------------------------------------------------------------------------------
  /*因爲還沒學Ajax就用session存放條件以記住查詢條件*/
        HttpSession session = req.getSession();
        session.setAttribute("name", name);
        session.setAttribute("sex", sex);
        session.setAttribute("maxPage", maxPage);
        return "person?action=getPerson";
    }
  • 得到最大頁後,再根據條件獲取記錄
public String getPerson(HttpServletRequest req,HttpServletResponse resp){
        PersonDao pd = new PersonDao();
        HttpSession session = req.getSession();
        //得到選擇條件後的最大頁,姓名,性別。會根據每次選擇條件的不同而改變。
        Integer maxPage = (Integer)session.getAttribute("maxPage");
        String name = (String)session.getAttribute("name");
        Integer sex = (Integer)session.getAttribute("sex");

        Integer currPage = 0;
        //從項目進來後,沒有從前臺獲得currPage,默認爲1
        if (req.getParameter("currPage") == null) { 
            currPage =1;
        }else{
        //點擊上下頁的時候找到此servlet,得到頁數。
------------------------------------------------------------
 <a href="person?action=getPerson&currPage=${currPage+1 }">下一頁</a>
------------------------------------------------------------
        currPage = Integer.valueOf(req.getParameter("currPage")); 
        }
            if(currPage < 1 ){
                currPage = 1;
            } else if(currPage > maxPage){
                currPage = maxPage;
            }
        //通過name,sex,當前頁,每頁顯示的條數導數據庫獲取數據。
        List<Person> list =  pd.getPerson(name, sex, currPage, rowPerPage);



--------------------------------------------------------------------------------

public List<Person> getPerson(String name, Integer sex,Integer currPage,Integer size){
        StringBuffer sb = new StringBuffer("select * from person where 1=1 ");
        ...
        //在這裏,我們把參數直接傳到Dao裏面來判斷。在我早先寫的那篇文章裏,是先在servlet判斷一次,又到Dao裏面判斷,實在是多餘了。不過"重構代碼"不也是一項必須的技能嗎。
        if(!(name.equals(null)) && !(name.equals("")) ){
            sb.append(" and name like '%"+name+"%' ");
        }
        if(sex != 0){
            sb.append(" and sex = "+sex+" ");
        }
        sb.append("  limit ?,? ");
        String sql = sb.toString();//要注意把sql語句轉爲String類型。
        try {
            conn = DataBaseUtil.getConnection();
            ps = conn.prepareStatement(sql);
            //這裏就只有兩個 ‘?’,不用再判斷有幾個 ‘?’了。
            ps.setInt(1, (currPage-1)*size);
            ps.setInt(2, size);
            rs = ps.executeQuery();
            while(rs.next()){
                ...
    }


--------------------------------------------------------------------------------
    //把得到的數據,送到前臺顯示。        
    if(list.size() > 0){
            req.setAttribute("pList", list);
            req.setAttribute("currPage", currPage);
            return "showPerson.jsp";
    }

早先寫的拼接Sql的文章(點我)

Ps:寫這篇文章的時候,被打斷了了好幾次,思路也是斷斷續續的。不過我會一直更新下去的,在學了Ajax,Jquery後。還有最好是寫一個框架來做這些事,不過對接口,反射的理解還不是太深。加油吧!

發佈了33 篇原創文章 · 獲贊 23 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章