JAVAWebページングでのクエリ機能の実装


クエリデータのページング表示実現するには、個人ブログ https://www.nfreak-man.cnを使用します。ページング表示機能に基づいてユーザー入力値を取得し、sqlステートメントをステッチする必要があります。これは、ページングデータの通常の取得には影響しません。ページング機能は、以前のブログで作成されました。

サーブレットの書き換え

取得した入力をマップセット条件に入れ、サービスクエリを呼び出してデータセットを返します。次に、データを表示するリクエストを通じて入力値をlist.jspに返す必要があります。

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取参数
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows");//每页显示条数
        if(currentPage == null || "".equals(currentPage)){
            currentPage = "1";
        }
        if(rows == null || "".equals(rows)){
            rows = "5";
        }

        //获取条件查询参数
        Map<String, String[]> codition = request.getParameterMap();

        //调用service查询
        UserSvice service = new UserServiceImpl();
        PageBean<User> pb = service.finUserByPage(currentPage,rows,codition);
    
        //将PageBean存入request
        request.setAttribute("pb",pb);
        request.setAttribute("condition",codition);//将查询条件存入request回显
        //转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

サービスの書き換え

daoを呼び出して、レコードの総数を照会してパラメーターの条件を増やします。条件に値がある場合、それは検索状態にあります。照会条件を満たすレコードの総数が取得されます。検索がない場合、レコードの総数を取得するために空になります。

@Override
public PageBean<User> finUserByPage(String _currentPage, String _rows, Map<String, String[]> codition) {
    int currentPage = Integer.parseInt(_currentPage);
    int rows = Integer.parseInt(_rows);
    if(currentPage <= 0){
        currentPage = 1;
    }
    //创建空的PageBean对象
    PageBean<User> pb = new PageBean<User>();
    //设置参数
    pb.setCurrentPage(currentPage);
    pb.setRows(rows);
    //调用dao查询总记录数
    int totalCount = dao.findTotalCount(codition);
    pb.setTotalCount(totalCount);
    //调用dao查询list集合
    //计算开始的记录的索引
    int start = (currentPage - 1) * rows;
    List<User> list = dao.findByPage(start,rows,codition);
    pb.setList(list);
    //计算总页码
    int totalPage = totalCount % rows == 0 ? totalCount/rows : totalCount/rows + 1;
    pb.setTotalPage(totalPage);
    return pb;
}

Daoメソッドの書き換え

元のSQLステートメントを書き換える後続の条件ステートメントを接合するために、where 1 = 1を追加します。条件の値が空でない場合、SQLステートメントのステッチが実行されます。

@Override
public List<User> findByPage(int start, int rows, Map<String, String[]> codition) {
    String sql = "select * from user where 1 = 1";
    StringBuilder sb = new StringBuilder(sql);
    //遍历map
    Set<String> Keyset = codition.keySet();
    //定义参数集合
    List<Object> params = new ArrayList<Object>();
    for (String key : Keyset) {
        //排除分页条件参数
        if ("currentPage".equals(key) || "rows".equals(key)){
            continue;
        }
        //获取value
        String value = codition.get(key)[0];
        //判断value是否有值
        if (value != null && !"".equals(value)){
            //有值
            sb.append(" and "+key+" like ? ");
            params.add("%"+value+"%");//?条件的值
        }
    }
    //添加分页查询
    sb.append(" limit ?,? ");
    //添加分页查询参数
    params.add(start);
    params.add(rows);
    
    return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray());
}

フロントエンドページの書き換え

クエリボックス:

<form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
    <div class="form-group">
        <label for="exampleInputName2">姓名</label>
        <input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" >
    </div>
    <div class="form-group">
        <label for="exampleInputName3">籍贯</label>
        <input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputName3" >
    </div>
    <div class="form-group">
        <label for="exampleInputEmail2">邮箱</label>
        <input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail2" >
    </div>
    <button type="submit" class="btn btn-default">查询</button>
</form>

ページングフレーム:

ユーザーが入力した値は、ページ番号、前のページ、次のページのクリックされたリンクに追加する必要があります。その後、見つかったページで通常のジャンプを実行できます。

<div>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <c:if test="${pb.currentPage == 1}"><li class="disabled"></c:if>
            <c:if test="${pb.currentPage != 1}"><li></c:if>
                <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <c:forEach begin="1" end="${pb.totalPage}" var="i">
                <c:if test="${pb.currentPage == i}">
                    <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
                </c:if>
                <c:if test="${pb.currentPage != i}">
                    <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
                </c:if>
            </c:forEach>
                <c:if test="${pb.currentPage == pb.totalPage}"><li class="disabled"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
                </li></c:if>
                <c:if test="${pb.currentPage < pb.totalPage}"><li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                </c:if>

            <span style="font-size: 25px;margin-left: 5px">
                共${pb.totalCount}条记录,共${pb.totalPage}页
            </span>
        </ul>
    </nav>
</div>
元の記事を28件公開 賞賛された0 訪問数722

おすすめ

転載: blog.csdn.net/William_GJIN/article/details/104998538