本次编码基于上一篇博文:
1.使用自定义标签实现分页效果,首先要新建一个java文件,继承SimpleTagSupport。
PageTag.java
package blank.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PageTag extends SimpleTagSupport {
private long nowPage;// 当前页
private long pages;// 总页数
private long pageSize;// 每页显示的数量
private long countSize;// 总记录数
private String url;// 访问的地址
// setter方法注入属性的值
public void setNowPage(long nowPage) {
this.nowPage = nowPage;
}
public void setPages(long pages) {
this.pages = pages;
}
public void setPageSize(long pageSize) {
this.pageSize = pageSize;
}
public void setCountSize(long countSize) {
this.countSize = countSize;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public void doTag() throws JspException, IOException {
// 获取输出流对象
JspWriter out = this.getJspContext().getOut();
out.print("<div>");
out.print("<a href='" + this.url + "?nowpage=1'>首页</a>");
out.print("<a href='" + this.url + "?nowpage=" + (this.nowPage - 1)
+ "'>上一页</a>");
out.print("<a href='" + this.url + "?nowpage=" + (this.nowPage + 1)
+ "'>下一页</a>");
out.print("<a href='" + this.url + "?nowpage=" + (this.pages)
+ "'>末页</a>");
out.print("</br>");
out.print("总共"+this.countSize+"记录,"+"当前是"+nowPage+"页,每页显示"+this.pageSize+"记录");
out.print("</div>");
}
}
2.在WEB-INF目录下添加该标签的描述文件
page.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
<!-- 描述信息 -->
<description>leo自定义的标签库</description>
<!-- 版本号 -->
<tlib-version>2.0</tlib-version>
<!-- 建议的名称 prefix="c" -->
<short-name>p</short-name>
<uri>/hytc</uri>
<tag>
<description>description</description>
<!-- 标签名称 -->
<name>page</name>
<!-- ip标签的时候 交给blank.tag.PageTag处理 -->
<tag-class>blank.tag.PageTag</tag-class>
<!-- 标签中的内容为scriptless -->
<body-content>empty</body-content>
<!-- 设置属性 -->
<attribute>
<name>nowPage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pageSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>countSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pages</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3.在之前的goods.jsp中引入自定义的标签库并使用自定义的page标签。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/hytc" prefix="p"%>
<% 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>My JSP 'goods.jsp' starting page</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>
<h1>显示商品信息</h1>
<div>
<table>
<tr>
<th>序号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>操作</th>
</tr>
<c:forEach var="g" items="${requestScope.pagination.rows }">
<tr>
<td>${g.id }</td>
<td>${g.name }</td>
<td>${g.price }</td>
<td>操作</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">
<%-- <a href="${pageContext.request.contextPath}/page1.do?nowpage=1">首页</a> <a href="${pageContext.request.contextPath}/page1.do?nowpage=${pagination.nowPage-1}">上一页</a> <a href="${pageContext.request.contextPath}/page1.do?nowpage=${pagination.nowPage+1}">下一页</a> <a href="${pageContext.request.contextPath}/page1.do?nowpage=${pagination.pages}">末页</a></td> --%>
<p:page nowPage="${pagination.nowPage}" pages="${pagination.pages}" pageSize="${pagination.pageSize}" countSize="${pagination.countSize}" url="${pageContext.request.contextPath}/page1.do"/>
</tr>
</table>
<div>项目路径:${pageContext.request.contextPath }</div>
</div>
</body>
</html>
4.实现效果:
版权声明:本文为博主原创文章,未经博主允许不得转载。