jsp查询到的所有结果用一张表格显示在一个页面上,我想在表格下方实现上一页,下一页翻页的功能按钮

2024-12-12 17:32:35
推荐回答(4个)
回答1:

jsp页面可以用分页来控制查询到的多条数据。
具体实例如下:
public class PageResult {
private List list; //装分页列表中的内容
private Integer firstPage; //第一页
private Integer prePage; //上一页
private Integer nextPage;//下一页
private Integer currentPage;//当前页面
private Integer totalPage; //总页数/尾页
private Integer count;//总条数
private Integer size; //每页多少条
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Integer getFirstPage() {
return firstPage;
}
public void setFirstPage(Integer firstPage) {
this.firstPage = firstPage;
}
public Integer getPrePage() {
return (this.currentPage-1==0?1:this.currentPage-1);
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return (this.currentPage==this.totalPage?this.totalPage:this.currentPage+1);
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalPage() {
return (this.count%this.size==0?this.count/this.size:this.count/this.size+1);
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

public class listServlet extends HttpServlet {

private IProductDAO dao;

@Override
public void init() throws ServletException {
dao = new IProductDAOImpl();
super.init();
}

货品列表



















货品名称
品牌
零售价






























货品零售价品牌折扣供应商操作
${product.productName}${product.salePrice}${product.brand}${product.cutoff}${product.supplier}删除 修改
首页${pageResult.firstPage}上一页${pageResult.prePage}下一页${pageResult.nextPage}尾页${pageResult.totalPage}当前${pageResult.currentPage}/${pageResult.totalPage}页总条数:${pageResult.count}





protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String currentPage = request.getParameter("currentPage");

PageResult pageResult = new PageResult();

if(StringUtils.isBlank(currentPage))
{
currentPage="1";
}

pageResult.setCurrentPage(Integer.parseInt(currentPage));

pageResult.setSize(5);

List list = dao.list(pageResult.getCurrentPage(), pageResult.getSize());

pageResult.setList(list);

int count = dao.count();

pageResult.setCount(count);

//如果当前页等于第一页,则上一页就是第一页,否则为当前页上一页
pageResult.setPrePage(pageResult.getPrePage());

//如果当前页是尾页,则下一页就是尾页,否则为当前页的下一页
pageResult.setNextPage(pageResult.getNextPage());

pageResult.setCount(count);

pageResult.setTotalPage(pageResult.getTotalPage());

request.setAttribute("pageResult", pageResult);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}

回答2:

建一个MySQL数据库test
数据库中有一个表:note,字段为:name(varchar)

< %@ page contentType="text/html;charset=gb2312" %>
< % java.sql.Connection sqlCon; //数据库连接对象
java.sql.Statement sqlStmt; //SQL语句对象
java.sql.ResultSet sqlRst; //结果集对象
java.lang.String strCon; //数据库连接字符串
java.lang.String strSQL; //SQL语句
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
java.lang.String strPage;
int i;
//设置一页显示的记录数
intPageSize = 2;
//取得待显示页码
strPage = request.getParameter("page");
if(strPage==null){
//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
} else{
//将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage< 1) intPage = 1;
}
//装载JDBC驱动程序
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//连接数据库
sqlCon= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test");
//创建语句对象
sqlStmt = sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.
ResultSet.CONCUR_READ_ONLY); //执行SQL语句
strSQL = "select name from note";
//执行SQL语句并获取结果集
sqlRst = sqlStmt.executeQuery(strSQL);
//获取记录总数
sqlRst.last();
intRowCount = sqlRst.getRow();
//记算总页数
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//调整待显示的页码
if(intPage>intPageCount) intPage = intPageCount;
%>
< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< title>JSP数据库操作例程 - JSP数据分页显示 - JDBC 2.0 - mysql< /title>
< /head>
< body>
< table border="1" cellspacing="0" cellpadding="0">
< tr>
< th>姓名< /th>
< /tr>
< % if(intPageCount>0)
{
//将记录指针定位到待显示页的第一条记录上
sqlRst.absolute((intPage-1) * intPageSize + 1);
//显示数据
i = 0;
while(i< intPageSize && !sqlRst.isAfterLast()){ %>
< tr>
< td>
< %=sqlRst.getString(1)%>
< /td>
< /tr>
< % sqlRst.next();
i++;
}
}
%>
< /table>
第< %=intPage%>页 共< %=intPageCount%>页
< %if(intPage< intPageCount){%>< a href="mysqlpage.jsp?page=< %=intPage+1%>">下一页< /a>< %}%>
< %if(intPage>1){%>< a href="mysqlpage.jsp?page=< %=intPage-1%>">上一页< /a>< %}%>
< /body>
< /html>
< %
//关闭结果集
sqlRst.close();
//关闭SQL语句对象
sqlStmt.close();
//关闭数据库
sqlCon.close();
%>

回答3:


回答4:

项目不完整,才5kb。怎么做得了?表结构也没有