jsp表单分层
想做一个JSP页面,怎么能做成分页的形式,求代码---急急 --------------------编程问答-------------------- 百度一下,像这种共同的需求应该很容易搜到的。。。 --------------------编程问答-------------------- 数据分页显示 还是? --------------------编程问答-------------------- 就是分页查询 --------------------编程问答-------------------- 自己写把,当初我也问过类似的问题,最后自己写出来了。给个思路:
如果用Mysql,可以通过select * from student limit 1,10,查出第1到第10条记录,往后类似。所以jsp页面只要提交pageId和pageSize就可以了,在后台则要计算limit beginrow,endrow的值。
oracle没有limit,只能用rownum,而且要嵌套的,自己搜一下吧。 --------------------编程问答-------------------- //查询当前页的商品记录
//pageSize 每页显示的记录数
//pageNow 当前页的页码
public ArrayList<Goods> getPageGoods(int pageSize,int pageNow){
ArrayList<Goods> al = null;
Connection conn = Conn.getConn();
String sql = "select Top " + pageSize
+ " * from Goods where goodsId not in("
+ " select Top " + pageSize*(pageNow -1)
+ " goodsId from Goods)";
System.out.println(sql);
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
al = new ArrayList<Goods>();
while(rs.next()){
Goods goods = new Goods();
goods.setGoodsId(rs.getInt("goodsId"));
goods.setGoodsName(rs.getString("goodsName"));
goods.setGoodsPhoto(rs.getString("goodsPhoto"));
goods.setGoodsPrice(rs.getString("goodsPrice"));
goods.setGoodsDesc(rs.getString("goodsDesc"));
al.add(goods);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return al;
}
//获取总页数的方法
//页数的计算
//如果 总记录数%pageSize == 0
//pageNum = 总记录数/pageSize
//否则pageNum = (int)(总记录数/pageSize)+1
public int getPageCount(int pageSize){
int pageNum = 0;//总页数
int pageCount = 0;//总记录数
Connection conn = Conn.getConn();
String sql = "select count(*) as count from Goods" ;
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
if(rs.next()){
pageCount = rs.getInt("count");
}
//计算页数的方法
if(pageCount%pageSize == 0)
pageNum = pageCount/pageSize;
else
pageNum = (int)(pageCount/pageSize)+1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pageNum;
}
JSP代码
<%@ page language="java" import="java.util.*,com.kp.entity.*,com.kp.conn.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>商品列表页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table cellspacing="15">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>描述</td>
<td>操作</td>
</tr>
<%GetGoods gg = new GetGoods();
int pageSize = 5;//每页显示5条记录
int pageNow = 1;//初始pageNow =1;
int pageNum = 0;//初始总页数
pageNum = gg.getPageCount(pageSize);
if(request.getParameter("pageNow") == null)
pageNow = 1;
else
pageNow = Integer.parseInt(request.getParameter("pageNow"));
if(pageNow <=0)
pageNow = 1;
if(pageNow >= pageNum)
pageNow = pageNum;
ArrayList<Goods> al = null;
al = gg.getPageGoods(pageSize,pageNow);//获取所有的商品
for(Goods goods:al){
%>
<tr>
<td><%=goods.getGoodsId() %></td>
<td><%=goods.getGoodsName() %></td>
<td><%=goods.getGoodsPrice() %></td>
<td><%=goods.getGoodsDesc() %></td>
<td><a href="addGoods.jsp?goodsId=<%=goods.getGoodsId() %>">放入购物车</a></td>
</tr>
<%} %>
<tr>
<td><a href="index.jsp?pageNow=1">首页</a></td>
<td><a href="index.jsp?pageNow=<%=pageNow-1 %>">上一页</a></td>
<td><a href="index.jsp?pageNow=<%=pageNow+1 %>">下一页</a></td>
<td><a href="index.jsp?pageNow=<%=pageNum %>">尾页</a></td>
</tr>
</table>
</body>
</html>
你自己再琢磨琢磨~~~ --------------------编程问答--------------------
关键的是方法和sql语句~~~
补充:Java , Web 开发