模板:分页JSP(结合Servlet)

DAO类(后续无需改变)

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Category;
import util.DBUtil;

public class CategoryDAO {
    public int getTotal(){
        String sql="select count(*) from category";
        int total=0;
        try(Connection c=DBUtil.getConnection();
                Statement s=c.createStatement();) {
            ResultSet rs=s.executeQuery(sql);
            while(rs.next()){
                total=rs.getInt(1);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return total;
    }
    
    public List<Category> list(int start,int count){
        List<Category> categories=new ArrayList<Category>();
        String sql="select * from category order by id limit ?,?";
        try (Connection c=DBUtil.getConnection();
                PreparedStatement ps=c.prepareStatement(sql);){
            ps.setInt(1, start);
            ps.setInt(2, count);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Category category=new Category();
                category.setId(rs.getInt(1));
                category.setName(rs.getString(2));
                categories.add(category);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return categories;
    }
}
View Code

效果一:列表全显示

 Servlet类

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Category;
import dao.CategoryDAO;

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        List<Category>categories=new CategoryDAO().list(0,Short.MAX_VALUE【可变】);
        request.setAttribute("categories【可变】", categories);
        request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
    }
}
View Code

JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed">
    <thead>【下面td内容可变】
        <tr class="success">
        <td>ID</td>
        <td>图片</td>
        <td>分类名称</td>
        <td>编辑</td>
        <td>删除</td>
    </tr>
    </thead>
    <tbody>
        <c:forEach  items="${categories【可变,取决前面servlet】}"  var="categorie【可变】">
        <tr>
            <td>${categorie.id【可变】}</td>
            <td><img height="40px" src="img/category/${categorie.id【可变】}.jpg"></td>
            <td>${categorie.name【可变】}</td>
            <td><a href="categoryEdit?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-edit"></span></a></td>
            <td><a href="categoryDelete?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-remove"></span></a></td>
        </tr>  
        </c:forEach>  
    </tbody>
</table>
View Code

效果二:只显示前5条数据

 Servlet类

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        int start=0;//【可变】
        int count=5;//【可变】
        List<Category>categories=new CategoryDAO().list(start,count);
        
        request.setAttribute("categories【可变】", categories);
        request.getRequestDispatcher("/admin/category/categoryList.jsp").forward(request, response);
    }
}
View Code

JSP代码

 //和上一个效果图的JSP代码一样 

效果三:可显示首页和末页

  Servlet类

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        int start=0;//起始页的start值
        int count=5;//【可变】
        
        try {//如果请求中不带参数start,必须要进行try块的错误处理
            start=Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // TODO: handle exception
        }
        int total=new CategoryDAO().getTotal();//总数
        int last=0;//末页的start值
        if(0==total%count)
            last=total - count;
        else
            last=total - total % count;
                
        
        List<Category>categories=new CategoryDAO().list(start,count);//【可变】
        request.setAttribute("last", last);//把last传递给jsp
        request.setAttribute("categories【可变】", categories);//把categories传递给jsp
        request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
    }
}
View Code

 JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed">
    <thead>【可变】
        <tr class="success">
        <td>ID</td>
        <td>图片</td>
        <td>分类名称</td>
        <td>编辑</td>
        <td>删除</td>
    </tr>
    </thead>
    <tbody>
        <c:forEach  items="${categories【可变】}"  var="categorie【可变】">
        <tr>
            <td>${categorie.id【可变】}</td>
            <td><img height="40px" src="img/category/${categorie.id【可变】}.jpg"></td>
            <td>${categorie.name【可变】}</td>
            <td><a href="【可变】categoryEdit?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-edit"></span></a></td>
            <td><a href="【可变】categoryDelete?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-remove"></span></a></td>
        </tr>  
        </c:forEach>  
    </tbody>
    <tfoot>
        <tr>
            <td colspan="6" align="center">
                <!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
                <a href="categoryList【可变】?start=0">[首页]</a>
                <a href="categoryList【可变】?start=${last}">[末页]</a>
            </td>
        </tr>
    </tfoot>
</table>
View Code

效果四:可显示首页和末页、下一页和上一页

 Servlet类

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        int start=0;//起始页的start值
        int count=5;//【可变】
        
        try {//如果请求中不带参数start,必须要进行try块的错误处理
            start=Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // TODO: handle exception
        }
        int total=new CategoryDAO().getTotal();//总数
        int last=0;//末页的start值
        if(0==total%count)
            last=total - count;
        else
            last=total - total % count;
 
        int pre=start-count;//上一页的start值
        pre=pre>0?pre:0;//边界处理
        int next=start+count;//下一页的start值
        next=next<last?next:last;//边界处理
        
        
        List<Category>categories=new CategoryDAO().list(start,count);//【可变】
        request.setAttribute("pre", pre);//把last传递给jsp
        request.setAttribute("next", next);//把last传递给jsp
        request.setAttribute("last", last);//把last传递给jsp
        request.setAttribute("categories【可变】", categories);//把categories传递给jsp
        request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
    }
}
View Code

JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed【可变】">
    <thead>
        <tr class="success【可变】">
        <td>ID</td>
        <td>图片</td>
        <td>分类名称</td>
        <td>编辑</td>
        <td>删除</td>
    </tr>
    </thead>
    <tbody>
        <c:forEach  items="${categories【可变】}"  var="categorie【可变】">
        <tr>
            <td>${categorie.id【可变】}</td>
            <td><img height="40px" src=【可变】"img/category/${categorie.id}.jpg"></td>
            <td>${categorie.name【可变】}</td>
            <td><a href=【可变】"categoryEdit?id=${categorie.id}"><span class="glyphicon glyphicon-edit"【可变】></span></a></td>
            <td><a href=【可变】"categoryDelete?id=${categorie.id}"><span class="glyphicon glyphicon-remove"【可变】></span></a></td>
        </tr>  
        </c:forEach>  
    </tbody>
    <tfoot>
        <tr>
            <td colspan="6" align="center">
                <!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
                <a href="categoryList?start=0">[首页]</a>
                <a href="categoryList?start=${pre}">[上一页]</a>
                <a href="categoryList?start=${next}">[下一页]</a>
                <a href="categoryList?start=${last}">[末页]</a>
            </td>
        </tr>
    </tfoot>
</table>
View Code

 效果五:可显示有序页

 Servlet类

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        int start=0;//起始页的start值
        int count=5;//【可变】
        
        try {//如果请求中不带参数start,必须要进行try块的错误处理
            start=Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // TODO: handle exception
        }
        int total=new CategoryDAO().getTotal();//总数
        int last=0;//末页的start值
        if(0==total%count)
            last=total - count;
        else
            last=total - total % count;
 
        
        
        int page=0;
        if(0==total%count)
            page=total % count;
        else
            page=total % count+1;
            
        List<Category>categories=new CategoryDAO().list(start,count);//【可变】
        request.setAttribute("last", last);//把count传递给jsp
        request.setAttribute("page", page);//把page传递给jsp
        request.setAttribute("count", count);//把count传递给jsp
        request.setAttribute("categories【可变】", categories);//把categories传递给jsp
        request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
    }
}
View Code

JSP代码

<table class=【可变】"table table-striped  table-bordered table-hover  table-condensed">
    <thead>【可变】
        <tr class=【可变】"success">
        <td>ID</td>
        <td>图片</td>
        <td>分类名称</td>
        <td>编辑</td>
        <td>删除</td>
    </tr>
    </thead>
    <tbody>
        <c:forEach  items="${categories【可变】}"  var="categorie【可变】">
        <tr>
            <td>${categorie.id【可变】}</td>
            <td><img height="40px" src=【可变】"img/category/${categorie.id}.jpg"></td>
            <td>${categorie.name【可变】}</td>
            <td><a href=【可变】"categoryEdit?id=${categorie.id}"><span class="glyphicon glyphicon-edit"></span></a></td>
            <td><a href=【可变】"categoryDelete?id=${categorie.id}"><span class="glyphicon glyphicon-remove"></span></a></td>
        </tr>  
        </c:forEach>  
    </tbody>
    <tfoot>
        <tr>
            <td colspan="6" align="center">
                <!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
                <a href=【可变】"categoryList?start=0">[1]</a>
                <c:forEach var="i" begin="2" end="${page-2}">
                    <a href=【可变】"categoryList?start=${(i-1)*count}">[${i}]</a>
                </c:forEach>
                <a href=【可变】"categoryList?start=${last}">[${page-1}]</a>
            </td>
        </tr>
    </tfoot>
</table>
View Code

  效果五:可显示首页、上一页、中间页、下一页、末页

Servlet类

public class CategoryListServlet extends HttpServlet{
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException{
        int start=0;//起始页开始值
        int count=5;//【可变】
        
        try {//如果请求中不带参数start,必须要进行try块的错误处理
            start=Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // TODO: handle exception
        }
        int total=new CategoryDAO().getTotal();//总数
        int last=0;//末页开始值
        if(0==total%count)
            last=total - count;
        else
            last=total - total % count;
 
        int pre=start-count;//上一页开始值
        pre=pre>0?pre:0;//边界处理
        int next=start+count;//下一页开始值
        next=next<last?next:last;//边界处理
        
        int page=0;//总页数
        if(0==total%count)
            page=total % count;
        else
            page=total % count+1;
            
        List<Category>categories=new CategoryDAO().list(start,count);//【可变】
        request.setAttribute("pre", pre);//把pre传递给jsp
        request.setAttribute("next", next);//把next传递给jsp
        request.setAttribute("last", last);//把last传递给jsp
        request.setAttribute("page", page);//把page传递给jsp
        request.setAttribute("count", count);//把count传递给jsp
        request.setAttribute("categories【可变】", categories【可变】);//把categories传递给jsp
        request.getRequestDispatcher("/admin/category/categoryList.jsp"【可变】).forward(request, response);
    }
}
View Code

JSP代码

<tfoot>
        <tr>
            <td colspan="6" align="center">
                <!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
                <a href=【可变】"categoryList?start=0">[首页]</a>
                <a href="categoryList?start=${pre}">[上一页]</a>
                <!-- 中间页 -->
                <a href=【可变】"categoryList?start=0">[1]</a>
                <c:forEach var="i" begin="2" end="${page-2}">
                    <a href=【可变】"categoryList?start=${(i-1)*count}">[${i}]</a>
                </c:forEach>
                <a href=【可变】"categoryList?start=${last}">[${page-1}]</a>
                <!-- 中间页 -->
                <a href=【可变】"categoryList?start=${next}">[下一页]</a>
                <a href=【可变】"categoryList?start=${last}">[末页]</a>
            </td>
        </tr>
    </tfoot>
View Code

 

posted @ 2020-05-21 16:08  Strugglinggirl  阅读(230)  评论(0编辑  收藏  举报