jsp查询到的所有结果用一张表格显示在一个页面上,我想在表格下方实现上一页,下一页翻页的功能按钮
怎么做啊,如图在表格下实现可以翻页的功能,而不是现在这样所有的结果显示在一起。谁帮我改下代码这是我的工程http://pan.baidu.com/s/1hqmrUFM谁有...
怎么做啊,如图
在表格下实现可以翻页的功能,而不是现在这样所有的结果显示在一起。谁帮我改下代码这是我的工程http://pan.baidu.com/s/1hqmrUFM谁有空有时间帮我看下,帮我改下,谢谢你们了,网上的我也看不懂 展开
在表格下实现可以翻页的功能,而不是现在这样所有的结果显示在一起。谁帮我改下代码这是我的工程http://pan.baidu.com/s/1hqmrUFM谁有空有时间帮我看下,帮我改下,谢谢你们了,网上的我也看不懂 展开
2016-01-23 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
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();
}
<title>货品列表</title>
</head>
<body>
<form action="/list" method="post">
<table border="1">
<tr>
<td>货品名称</td>
<td><input type="text" name="productName"></td>
</tr>
<tr>
<td>品牌</td>
<td><input type="text" name="brand"></td>
</tr>
<tr>
<td>零售价</td>
<td><input type="text" name="salePrice"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="查询"></td>
</tr>
</table>
</form>
<table border="1">
<tr>
<th>货品</th>
<th>零售价</th>
<th>品牌</th>
<th>折扣</th>
<th>供应商</th>
<th>操作</th>
</tr>
<c:forEach items="${pageResult.list}" var="product">
<tr>
<td>${product.productName}</td>
<td>${product.salePrice}</td>
<td>${product.brand}</td>
<td>${product.cutoff}</td>
<td>${product.supplier}</td>
<td><a href='/employee?cmd=remove&id=${employee.id}'>删除</a> <a href='/employee?cmd=edit&id=${employee.id}'>修改</a></td>
</tr>
</c:forEach>
<tr>
<th><a href="/list?currentPage=${pageResult.firstPage}">首页</a>${pageResult.firstPage}</th>
<th><a href="/list?currentPage=${pageResult.prePage}">上一页</a>${pageResult.prePage}</th>
<th><a href="/list?currentPage=${pageResult.nextPage}">下一页</a>${pageResult.nextPage}</th>
<th><a href="/list?currentPage=${pageResult.totalPage}">尾页</a>${pageResult.totalPage}</th>
<th>当前${pageResult.currentPage}/${pageResult.totalPage}页</th>
<th>总条数:${pageResult.count}</th>
</tr>
</table>
</body>
</html>
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<Product> 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);
}
}
具体实例如下:
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();
}
<title>货品列表</title>
</head>
<body>
<form action="/list" method="post">
<table border="1">
<tr>
<td>货品名称</td>
<td><input type="text" name="productName"></td>
</tr>
<tr>
<td>品牌</td>
<td><input type="text" name="brand"></td>
</tr>
<tr>
<td>零售价</td>
<td><input type="text" name="salePrice"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="查询"></td>
</tr>
</table>
</form>
<table border="1">
<tr>
<th>货品</th>
<th>零售价</th>
<th>品牌</th>
<th>折扣</th>
<th>供应商</th>
<th>操作</th>
</tr>
<c:forEach items="${pageResult.list}" var="product">
<tr>
<td>${product.productName}</td>
<td>${product.salePrice}</td>
<td>${product.brand}</td>
<td>${product.cutoff}</td>
<td>${product.supplier}</td>
<td><a href='/employee?cmd=remove&id=${employee.id}'>删除</a> <a href='/employee?cmd=edit&id=${employee.id}'>修改</a></td>
</tr>
</c:forEach>
<tr>
<th><a href="/list?currentPage=${pageResult.firstPage}">首页</a>${pageResult.firstPage}</th>
<th><a href="/list?currentPage=${pageResult.prePage}">上一页</a>${pageResult.prePage}</th>
<th><a href="/list?currentPage=${pageResult.nextPage}">下一页</a>${pageResult.nextPage}</th>
<th><a href="/list?currentPage=${pageResult.totalPage}">尾页</a>${pageResult.totalPage}</th>
<th>当前${pageResult.currentPage}/${pageResult.totalPage}页</th>
<th>总条数:${pageResult.count}</th>
</tr>
</table>
</body>
</html>
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<Product> 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);
}
}
展开全部
建一个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();
%>
数据库中有一个表: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();
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询