首页 > 代码库 > java超强分页标签演示
java超强分页标签演示
最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!、
以下是java标签类,继承自SimpleTagSupport
[java] view plaincopyprint?
- package com.lynn.oa.tag;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspWriter;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- import com.lynn.oa.util.Page;
- public class PageTag extends SimpleTagSupport {
- private Integer pageIndex;//当前页数
- private Integer pageCount;//总页数
- private String path;//页面链接
- private String param;//传入的参数
- //private List<Page> pageList;//显示的页号,一次显示10页,当前页位于中间第5的位置
- @Override
- public void doTag() throws JspException, IOException {
- JspWriter out = this.getJspContext().getOut();//指定输入流,用于页面输出分页信息、
- StringBuffer sb = new StringBuffer();//构建StringBuffer对象,用户拼接分页标签
- sb.append(this.getPageIndex());
- sb.append("/");
- sb.append(this.getPageCount());
- //显示当前页和总页数
- //如果当前页在第一页,则首页和上一页没有超链接
- if(this.pageIndex==1){
- sb.append("首页 上一页 ");
- }else{
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(1);
- sb.append("‘>首页</a> ");
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(this.pageIndex-1);
- sb.append("‘>上一页</a> ");
- }
- //下面的代码显示页码,当前页在中间位置,
- if(this.getPageCount()<=10){
- for (int i = 1; i <= this.getPageCount(); i++) {
- //如果页数小于等于10页,则全部显示
- if( i == pageIndex){//如果页码等于当前页,则该页数没有超链接
- sb.append("[");
- sb.append(i);
- sb.append("] ");
- }else{//否则给出超链接
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(i);
- sb.append("‘>[");
- sb.append(i);
- sb.append("]</a> ");
- }
- }
- }else{//如果大于10页,则从当前页为中心只显示其中10页
- int index=1;
- if(this.pageIndex>4){//并且如果当前页大于4页,从当前页前4页开始显示10个页数
- for (int j = this.pageIndex-4;j<=this.getPageCount(); j++) {
- if(j==this.pageIndex){//如果页码等于当前页,则该页数没有超链接
- sb.append("[");
- sb.append(j);
- sb.append("] ");
- }else{//否则给定超链接
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(j);
- sb.append("‘>[");
- sb.append(j);
- sb.append("]</a> ");
- }
- index++;
- if(index>10)//如果循环到10次则退出循环
- break;
- }
- }else{
- for (int i = 1; i <= this.getPageCount(); i++) {
- //如果页数小于等于10页,则全部显示
- if( pageIndex.equals(i)){//如果页码等于当前页,则该页数没有超链接
- sb.append("[");
- sb.append(i);
- sb.append("] ");
- }else{//否则给出超链接
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(i);
- sb.append("‘>[");
- sb.append(i);
- sb.append("]</a> ");
- }
- index++;
- if(index>10)
- break;
- }
- }
- }
[java] view plaincopyprint?
- //如果当前页是最后一页,则末页和下一页没有超链接
- if(this.pageIndex.equals(this.pageCount)){
- sb.append("下一页 末页 ");
- }else{
- sb.append("<a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(this.pageIndex+1);
- sb.append("‘>下一页</a>");
- sb.append(" <a href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append(this.pageCount);
- sb.append("‘>末页</a>");
- }
- //System.out.prIntegerln(sb.toString());
- //一下代码拼接转到第几页的js代码
- sb.append("<script type=‘text/javascript‘>");
- sb.append("function getPage(){");
- sb.append("var pageIndex = document.getElementById(‘pageText‘).value;");
- sb.append("window.location.href=http://www.mamicode.com/‘");
- sb.append(this.getPath());
- sb.append("?");
- sb.append(this.param);
- sb.append("=");
- sb.append("‘+pageIndex;}");
- sb.append("</script>");
- sb.append(" 转到第<input type=‘text‘ name=‘pageText‘ id=‘pageText‘ style=‘width:15px;‘ />页");
- sb.append("<input type=‘button‘ name=‘pageBtn‘ value=http://www.mamicode.com/‘GO‘ onclick=‘getPage()‘ />");
- sb.append("<script type=‘text/javascript‘>");
- sb.append("$(function(){$(‘#pageSize‘).change(function(){var pageSize=$(‘#pageSize‘).val();window.location.href=http://www.mamicode.com/‘");
- sb.append(this.path);
- sb.append("?");
- sb.append("pageIndex=1&index=‘+pageSize});});</script>");
- sb.append("每页显示<select id=‘pageSize‘><option id=‘noone‘ value=http://www.mamicode.com/‘1‘>请选择");
- sb.append("<option value=http://www.mamicode.com/‘1‘>1");
- sb.append("<option value=http://www.mamicode.com/‘5‘>5");
- sb.append("<option value=http://www.mamicode.com/‘10‘>10页");
- out.print(sb);
- }
- public Integer getPageIndex() {
- return pageIndex;
- }
- public void setPageIndex(Integer pageIndex) {
- this.pageIndex = pageIndex;
- }
- public Integer getPageCount() {
- return pageCount;
- }
- public void setPageCount(Integer pageCount) {
- this.pageCount = pageCount;
- }
- public String getPath() {
- return path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- public String getParam() {
- return param;
- }
- public void setParam(String param) {
- this.param = param;
- }
- public static void main(String[] args) {
- PageTag pt = new PageTag();
- pt.setPageIndex(13);
- pt.setPageCount(13);
- //pt.setPageList(pageList)
- pt.setPath("index.jsp");
- pt.setParam("pageIndex");
- try {
- pt.doTag();
- } catch (JspException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
tld文件如下:
[java] view plaincopyprint?
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglib xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
- version="2.1">
- <description>JSTL 1.1 core library</description>
- <display-name>JSTL core</display-name>
- <tlib-version>1.0</tlib-version>
- <short-name>mt</short-name>
- <uri>http://www.lynn88.com/jsp/jstl/pagetag</uri>
- <tag>
- <description>
- Catches any Throwable that occurs in its body and optionally
- exposes it.
- </description>
- <name>page</name>
- <tag-class>com.lynn.oa.tag.PageTag</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>pageIndex</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- <type>java.lang.Integer</type>
- </attribute>
- <attribute>
- <name>pageCount</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- <type>java.lang.Integer</type>
- </attribute>
- <attribute>
- <name>path</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- <type>java.lang.String</type>
- </attribute>
- <attribute>
- <name>param</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- <type>java.lang.String</type>
- </attribute>
- </tag>
- </taglib>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。