您的位置:首页 > 编程语言 > Java开发

Servlet+JavaBean+JSP真假分页技术详解

2014-08-21 15:44 615 查看
说明:分页技术分为真分页和假分页,具体采用哪种技术需要根据需求自我抉择。其实两者之间实现区别并不是太大。在分页之前我们需要搞明白对谁进行分页,一般情况是将数据封装到一个list集合中,明白这这一点问题基本上就已经解决了。(编写匆促如有错误请联系我)下面首先介绍真分页。
方法一:
为了大家学习起来方便, 我将在项目中用到的内容都放在这个文档中,所以可能会比较萝莉啰嗦。
1.构建数表,字段如下

goods
goodidint
goodnamevarchar(45)
priceflaot
2,建立实体类

privateintgoodsid;//物品编号
private String goodsname;
privatefloatprice;//单价
//省略getXXX,与setXXX方法


3,编写utils工具类,实现数据库的连接(该工具类比较粗糙,没有借鉴的价值,之方便学习分页使用)
publicclass DbConnection {
privatestatic Connection conn=null;
private String driver="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/mydata";
private String user="root";
private String password="123";
private DbConnection()
{
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
}
publicstatic ConnectiongetConnection()
{
if(conn==null)
new DbConnection();
returnconn;
}
}

4,编写DAO层
publicclass Dao {
privatestatic Connection conn;
privatestatic ResultSet rs=null;
privatestatic Statement stmt,stmt1;
privatestaticintpagesize=5;
static  {
conn=DbConnection.getConnection();

}
publicstatic ResultSetExecuteQuery(String sql)
{
try {
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
} catch (SQLException e){
e.printStackTrace();
}
returnrs;
}
//分页逻辑
publicstatic  ArrayList<Goods> getGoodsList(int currentpageno) throws SQLException
{
ArrayList<Goods>GoodsList=newArrayList<Goods>();
int BeginRecord=(currentpageno-1)*pagesize;
int EndRecord=pagesize;//备注,注意,该处表示的是从BeginRecord开始查询几个数据,而不代表结束的位置。
rs=ExecuteQuery("select *from goods limit "+BeginRecord+","+EndRecord);
try {
while(rs.next())
{
Goodsgoods=new Goods();
goods.setGoodsid(rs.getInt(1));
goods.setGoodsname(rs.getString(2));
goods.setPrice(rs.getFloat(3));
GoodsList.add(goods);
}
} catch (SQLException e){
e.printStackTrace();
}
return GoodsList;

}
//返回总页数
publicstaticint getPageCount()
{
int total=0;
int PageCount=0;
try {
rs=stmt.executeQuery("selectcount(*) from goods");
if(rs.next())
{
total=rs.getInt(1);
PageCount=(total-1)/pagesize+1;
}
} catch (SQLException e){
e.printStackTrace();
}
return PageCount;
}
}


5,servlet层
publicvoiddoPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
Stringpageno=request.getParameter("currentpageno");
int currentpageno=1;
if(pageno!=null){
currentpageno=Integer.parseInt(pageno);
}
ArrayList<Goods>GoodsList=null;
try {
GoodsList = Dao.getGoodsList(currentpageno);
System.out.println(GoodsList.size());
} catch (SQLException e){
e.printStackTrace();
}
request.setAttribute("GoodsList", GoodsList);
request.setAttribute("currentpageno", currentpageno);
request.setAttribute("PageCount",Dao.getPageCount());//设置总共页数
request.getRequestDispatcher("/LimiteGoods.jsp").forward(request, response);
}
6,jsp表示层
<%@ pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>分页技术Demo</title>
<script type="text/javascript">
function jumptopage()
{

var inputnode=document.getElementById("jumptopage");
var page=inputnode.value;
if(isNaN(page))
{
alert("请输入正整数");
}
else if(page>${PageCount})
return;
else
{
window.location="/Mypage/servlet/GetLimiteGoods?currentpageno="+page;
}
}

</script>
</head>
<body>
<center>

<table width="80%" border="1" height="56">
<tr align="center">
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<c:forEach var="goods" items="${GoodsList}">
<tr align="center">
<td>
${goods.goodsid}
</td>
<td>
${goods.goodsname}
</td>
<td>
${goods.price}
</td>
</tr>
</c:forEach>
</table>
<c:if test="${currentpageno>1}">
<a href="/Mypage/servlet/GetLimiteGoods?currentpageno=1">首页</a>
<a href="/Mypage/servlet/GetLimiteGoods?currentpageno=${currentpageno-1}">上一页</a>
</c:if>
<font color="blue">当前为 ${currentpageno}页</font>
<c:if test="${PageCount>currentpageno}">
<a href="/Mypage/servlet/GetLimiteGoods?currentpageno=${currentpageno+1}">下一页</a>
<a href="/Mypage/servlet/GetLimiteGoods?currentpageno=${PageCount}">尾页</a>
</c:if>
跳转到第<input type="text" id="jumptopage"   style="width:15px;">页
<input type="button" value="go" onclick="jumptopage()">
</center>
</body>
</html>


真分页部分结束
第二部分,假分页的实现
1,数据表,Bean,util与第一部分相同,不再重复
2,dao层做稍微的修改
publicstatic  ArrayList<Goods> getGoodsList(int currentpageno) throws SQLException
{
ArrayList<Goods>GoodsList=newArrayList<Goods>();
rs=ExecuteQuery("select * from goods");//改变的地方
try {
while(rs.next())
{
Goodsgoods=new Goods();
goods.setGoodsid(rs.getInt(1));
goods.setGoodsname(rs.getString(2));
goods.setPrice(rs.getFloat(3));
GoodsList.add(goods);
}
} catch (SQLException e){
e.printStackTrace();
}
return GoodsList;
}
Dao层返回页数部分函数不再需要
3,servlet层部分修改
publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
int pagesize=5;//增加项
int pageCount;//增加项
Stringpageno=request.getParameter("currentpageno");
int currentpageno=1;
if(pageno!=null){
currentpageno=Integer.parseInt(pageno);
}
ArrayList<Goods>GoodsList=null;
ArrayList<Goods>gl=newArrayList<Goods>();//增加项
try {
GoodsList = Dao.getGoodsList(currentpageno);
Iterator<Goods>it=GoodsList.iterator();
//---------------------------------修改部分
int i=0;
for(i=0;i<(currentpageno-1)*5;i++)
it.next();
for(i=0;i<5;i++)
{

gl.add((Goods)it.next());
}
//---------------------------------修改部分
} catch (SQLException e){
e.printStackTrace();
}
pageCount=(GoodsList.size()-1)/pagesize+1;
request.setAttribute("GoodsList", gl);//修改部分
request.setAttribute("currentpageno", currentpageno);
request.setAttribute("PageCount",pageCount );
request.getRequestDispatcher("/LimiteGoods.jsp").forward(request,response);
}

4,jsp层和上文相同,不再重复。
5,另附web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5">
<display-name>Mypage</display-name>
<servlet>
<servlet-name>GetLimiteGoods</servlet-name>
<servlet-class>servlet.GetLimiteGoods</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetLimiteGoods</servlet-name>
<url-pattern>/servlet/GetLimiteGoods</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>servlet/GetLimiteGoods</welcome-file>
<welcome-file>servlet/GetLimiteGoods</welcome-file>
<welcome-file>servlet/GetLimiteGoods</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>CharFilter</display-name>
<filter-name>CharFilter</filter-name>
<filter-class>Filter.CharFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


本文出自 “月芽之上” 博客,请务必保留此出处http://yueyazhishang.blog.51cto.com/7392875/1543149
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: