上次我们讲到了后台的分页,今天我们讲讲前台是怎么做分页的
1.首先我们创建一个类
package tags;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.zking.util.PageBean;
public class PageTag extends BodyTagSupport {
private static final long serialVersionUID = 9185674235633167393L;
private PageBean pageBean;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
//下一次请求提交到后台的表单html代码拼接
sb.append(" <form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>");
Map<String, String[]> paramMap = pageBean.getParamMap();
if(paramMap != null &¶mMap.size() >0) {
// Set<Entry<String,String[]>> entrySet = paramMap.entrySet();
for(Entry<String,String[]> entry :paramMap.entrySet()) {
if(!"page".equals(entry.getKey())) {
for(String val : entry.getValue()) {
sb.append(" <input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>");
}
}
}
}
sb.append(" <input type='hidden' name='page'>");
sb.append(" </form>");
//分页条html代码拼接
sb.append(" <div style='text-align: right; font-size: 12px;'>");
sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a ");
sb.append(" href='javascript:gotoPage(1)'>首页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getPreviouspage()+")'>上一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a> <input type='text'");
sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'> <a");
sb.append(" href='javascript:skipPage()'>Go</a>");
sb.append(" </div>");
//分页所需要调用的js代码
sb.append("<script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append("</script>");
return sb.toString();
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
}
2.然后写一个标签库
<name>page</name>
<tag-class>tags.PageTag</tag-class>
<!-- JSP -->
<body-content>JSP</body-content>
<attribute>
<name>pageBean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
3.获取url请求的参数和添加属性
package com.zking.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
private String url;
private Map<String, String[]> paramMap = new HashMap<>();
/**
* 获取URl请求的所有参数
* 添加属性
* @param req
*/
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));
this.setRows(req.getParameter("rows"));
this.setPagination(req.getParameter("pagination"));
//getRequestURL获取到浏览器请求的全路径
this.setUrl(req.getRequestURL().toString());
//getParameterMap可以获取到一次url请求所携带的所有参数
this.setParamMap(req.getParameterMap());
}
private void setPagination(String pagination) {
if(StringUtils.isBlank(pagination)) {
this.setPagination(!"false".equals(pagination));
}
}
public void setRows(String rows) {
if(StringUtils.isBlank(rows)) {
this.setRows(Integer.valueOf(rows));
}
}
public void setPage(String page) {
if(StringUtils.isBlank(page)) {
this.setPage(Integer.valueOf(page));
}
}
public PageBean() {
super();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String[]> paramMap) {
this.paramMap = paramMap;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
/**
* 获取到总页数
* @return
*/
public int getMaxPage() {
return this.total/ this.rows == 0 ? this.total / this.rows : (this.total%this.rows)+1;
}
/**
* 获取下一页页码
* @return
*/
public int getNextPage() {
return this.page < this.getMaxPage() ? this.page+1 : this.page;
}
/**
* 获取上一页页码
* @return
*/
public int getPreviouspage() {
return this.page > 1 ? this.page : this.page;
}
}
4.开始进行逻辑处理
package web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.BookDao;
import com.zking.entity.Book;
import com.zking.util.PageBean;
public class BookServlet extends HttpServlet {
private BookDao bookdao = new BookDao();//private私有化,不能被外部访问!!!
private static final long serialVersionUID = -4405443018425237315L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String bname = req.getParameter("bname");
Book book = new Book();
book.setBname(bname);
PageBean pagebean = new PageBean();
pagebean.setRequest(req);//将数据存到pagebean里面
try {
// 查询出符合条件的某一页的记录数
List<Book> list = bookdao.list(book, pagebean);
req.setAttribute("page", "pagebean");
req.getRequestDispatcher("bookList.jsp").forward(req, resp);//转发
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.配置xml
<filter>
<filter-name>EncodingFiter</filter-name>
<filter-class>com.zking.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>web.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bookServler</servlet-name>
<url-pattern>/bookServler</url-pattern>
</servlet-mapping>
</web-app>
6.进行前台界面的处理
1.这个是没传值的界面!!!
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/bookServlet.action"
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
</form>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
</tr>
<c:forEach items="${bookList }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
</tr>
</c:forEach>
</table>
<form id='pageBeanForm' action='' method='post'>
<input type='hidden' name=''> <input type='hidden' name='page'>
</form>
<div style='text-align: right; font-size: 12px;'>
每页N条,共N条,第N页,共N页 <a href='javascript:gotoPage(?)'>首页</a> <a
href='javascript:gotoPage(?)'>上一页</a> <a
href='javascript:gotoPage(?)'>下一页</a> <a
href='javascript:gotoPage(?)'>尾页</a> <input type='text'
id='skipPage'
style='text-align: center; font-size: 12px; width: 50px;'> <a
href='javascript:skipPage()'>Go</a>
</div>
<script type='text/javascript'>
function gotoPage(page) {
document.getElementById('pageBeanForm').page.value = page;
document.getElementById('pageBeanForm').submit();
}
function skipPage() {
var page = document.getElementById('skipPage').value;
if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>maxPage){
alert('请输入1~N的数字');
return;
}
gotoPage(page);
}
</script>
2.这个是已经弄好的界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/zking" prefix="z" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/bookServlet"
method="post">
<!-- <input type="hidden" name="rows" value="20"> -->
<input type="hidden" name="pagination" value="false">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
</form>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
</tr>
<c:forEach items="${bookList }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pagebean }"></z:page>
</body>
</html>
今天的分享就到此结束,欢迎下次在来!!!