志在指尖
用双手敲打未来

jsp分页(jsp分页功能如何实现)

jsp分页

通用分页的核心
.将上一次查询请求再发一次,只不过改变了页码。
本章重点:
1、改造上次PageBean里的内容;
2、页面展示
3、创建控制层servlet
4、自定义分页标签
5、助手类信息
1、补全后的PageBean:
packagecom.yuan.util;
importjava.util.HashMap;
importjava.util.Map;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
/**
*分页工具类
*
*/
publicclassPageBean{
privateintpage=1;//页码
privateintrows=10;//页大小
privateinttotal=0;//总记录数
privatebooleanpagination=true;//是否分页
privateMap<String,String[]>paMap=newHashMap<>();
privateStringurl;
publicvoidsetRequest(HttpServletRequestreq){
Map<String,String[]>parameterMap=req.getParameterMap();
StringBufferurl=req.getRequestURL();
//保存上一次请求所携带的参数
this.setPaMap(req.getParameterMap());
this.setUrl(req.getRequestURL().toString());
//在jsp页面来控制是否分页
//this.setPagination(req.getParameter(“pagination”));
//在jsp页面控制一页展示多少条信息
//this.setRows(req.getParameter(“rows”));
this.setPage(req.getParameter(“page”));
}
//setPage重载
privatevoidsetPage(Stringpage){
this.page=StringUtils.isNotBlank(page)?Integer.valueOf(page):this.page;
}
//setPagination重载
publicvoidsetPagination(Stringpagination){
this.pagination=StringUtils.isNotBlank(pagination)?!”false”.equals(pagination):this.pagination;
}
//setRows重载
publicvoidsetRows(Stringrows){
this.rows=StringUtils.isNotBlank(rows)?Integer.valueOf(rows):this.rows;
}
publicMap<String,String[]>getPaMap(){
returnpaMap;
}
publicvoidsetPaMap(Map<String,String[]>paMap){
this.paMap=paMap;
}
publicStringgetUrl(){
returnurl;
}
publicvoidsetUrl(Stringurl){
this.url=url;
}
publicPageBean(){
super();
}
publicintgetPage(){
returnpage;
}
publicvoidsetPage(intpage){
this.page=page;
}
publicintgetRows(){
returnrows;
}
publicvoidsetRows(introws){
this.rows=rows;
}
publicintgetTotal(){
returntotal;
}
publicvoidsetTotal(inttotal){
this.total=total;
}
publicvoidsetTotal(Stringtotal){
this.total=Integer.parseInt(total);
}
publicbooleanisPagination(){
returnpagination;
}
publicvoidsetPagination(booleanpagination){
this.pagination=pagination;
}
/**
*获得起始记录的下标
*
*@return
*/
publicintgetStartIndex(){
return(this.page-1)*this.rows;
}
@Override
publicStringtoString(){
return”PageBean[page=”+page+”,rows=”+rows+”,total=”+total+”,pagination=”+pagination+”]”;
}
/**
*获取最大页码数
*@return
*/
publicintgetMaxPage(){
returnthis.total%this.rows==0?this.total/this.rows:this.total/this.rows+1;
}
/**
*下一页
*@return
*/
publicintgetNexPage(){
returnthis.page<this.getMaxPage()?this.page+1:this.page;
}
/**
*上一页jsp
*@return
*/
publicintgetPreviousPage(){
returnthis.page>1?this.page-1:this.page;
}
}
2、jsp页面展示,分页代码插入到了控制层的Servlet里面
<%@pagelanguage=”java”contentType=”text/html;charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@pageisELIgnored=”false”%><!–中途遇到EL表达式(${})失效,插入此代码解决–>
<%@taglibprefix=”z”uri=”/yuan”%><!–导入自定义标签库使用自定义的分页标签–>
<%@taglibprefix=”c”uri=”http://java.sun.com/jsp/jstl/core”%>
<!DOCTYPEhtmlPUBLIC”-//W3C//DTDHTML4.01Transitional//EN””http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<metahttp-equiv=”Content-Type”content=”text/html;charset=UTF-8″>
<title>Inserttitlehere</title>
</head>
<body>
<c:iftest=”${emptybookList}”>
<jsp:forwardpage=”bookServlet.action”></jsp:forward>
</c:if>
<h2>小说目录</h2>
<br>
<formaction=”${pageContext.request.contextPath}/bookServlet.action”
method=”post”>
书名:<inputtype=”text”name=”bname”><inputtype=”submit”
value=”确定”>
<inputtype=”hidden”name=”pagination”value=”false”>
<inputtype=”hidden”name=”rows”value=”20″>
</form>
<tableborder=”1″width=”100%”>
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
</tr>
<c:forEachitems=”${bookList}”var=”s”>
<tr>
<td>${s.bid}</td>
<td>${s.bname}</td>
<td>${s.price}</td>
</tr>
</c:forEach>
</table>
<z:PagepageBean=”${pageBean}”></z:Page>
</body>
</html>
复制代码
3、创建控制层Servlet
packagecom.web;
importjava.io.IOException;
importjava.sql.SQLException;
importjava.util.List;
importjava.util.Map;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.yuan.dao.BookDao;
importcom.yuan.entity.Book;
importcom.yuan.util.PageBean;
publicclassBookServletextendsHttpServlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateBookDaobookDao=newBookDao();
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
doPost(req,resp);
}
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
Stringbname=req.getParameter(“bname”);
Bookbook=newBook();
book.setBname(bname);
//
PageBeanbean=newPageBean();
try{
bean.setRequest(req);
List<Book>list=this.bookDao.list(book,bean);
req.setAttribute(“bookList”,list);
req.setAttribute(“pageBean”,bean);
req.getRequestDispatcher(“/BookList.jsp”).forward(req,resp);
}catch(InstantiationException|IllegalAccessException|SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}

jsp分页功能如何实现

1、主要步骤:
首先,通过sql数据来计算数据库中的数据条数,具体的代码如下
Stringsql_count=”selectcount(*)fromarticleawhere1=1″;
//然后通过数据库操作获取总条数count
intcount=0;
//(数据库连接过程省略)
Resultrs=stmt.executeQuery(sql_count);
if(rs.next()){
count=rs.getInt(1);
}
注意,这里有个小小的细节:where1=1的作用是什么?
初看这个条件好像是多于的,但是,其实它对于拼接sql字符有很方便的作用,这是为了方便在拼接字符时出现条件判断的时候无法判断下一个是否需要拼接where字符,所以统一用在这之前加一个“多于”的where1=1方便操作。
2、算好总数据条数之后,我们就可以根据每页条数进行总页数等变量的计算了,具体请看一下代码
//根据总条数来进行计算相关分页的数据
//定义相关变量
intpages=0;//当前页码数
inttotalPages=0;//总页数
intlimit=5;//每页的条数
//计算
if(count%limit==0){
totalPages=count/limit
}else{
totalPages=count/limit+1;
}jsp1
以上并没有涉及新技术,具体请参考代码
3、最后,就是页面的前端操作了,具体就不累赘了。
其实总的来说,jsp分页的技术并没有什么新的技术在里面,纯粹是各种基本的知识来实现的,所以以上的总结显得比较简单。

未经允许不得转载:IT技术网站 » jsp分页(jsp分页功能如何实现)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载