⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagertag.java

📁 自己写的一个struts+spring+hibernate测试程序
💻 JAVA
字号:
/*
 * Created on 2006-2-27
 */
package com.common.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.common.utils.Page;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author tommy.zeng
 * @version 1.0
 */
public class PagerTag extends TagSupport{
	private static final long serialVersionUID = -5865025519615146506L;
	private int pageNo = 1;
    private int pageSize = 15;
    private int totalRecords;
    
    private String url;
    
    private String name;

    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }
    /**
     * @param name The name to set.
     */
    public void setName(String name) {
        this.name = name;
    }
    public PagerTag() {
    }


    /**
     * Process the end tag for this instance.
     *
     * @throws JspException
     * @return int
     * @todo Implement this javax.servlet.jsp.tagext.Tag method
     */
    public int doEndTag() throws JspException {
      return 0;
    }


    /**
     * Process the start tag for this instance.
     *
     * @throws JspException
     * @return int
     * @todo Implement this javax.servlet.jsp.tagext.Tag method
     */
    public int doStartTag() throws JspException {
      try {
        Page page = (Page)this.pageContext.getRequest().getAttribute(name);
        
        if(null!=page) {
            pageNo = page.getPageNo();
            pageSize = page.getPageSize();
            totalRecords = page.getTotalRecords();
        }
        if(totalRecords>0) {
	        JspWriter out = this.pageContext.getOut();
	        out.print("共"+totalRecords +"条记录&nbsp;&nbsp"+pageNo + "/" +getTotalPage() + "&nbsp;&nbsp;");
	        out.print(getFirstPage());
	        out.print(" | ");
	        out.print(getPreviousPage());
	        out.print(" | ");
	        out.print(getNextPage());
	        out.print(" | ");
	        out.print(getLastPage());
	        out.print("&nbsp; 跳至 ");
	        out.print(getGoPage());
        }
      }
      catch(Exception e){
        e.printStackTrace();
      }
      return 0;
    }

    /**
     * @return Returns the pageNo.
     */
    public int getPageNo() {
        return pageNo;
    }
    /**
     * @param pageNo The pageNo to set.
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
    /**
     * @return Returns the pageSize.
     */
    public int getPageSize() {
        return pageSize;
    }
    /**
     * @param pageSize The pageSize to set.
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    
    /**
     * @return Returns the url.
     */
    public String getUrl() {
        return url;
    }
    /**
     * @param url The url to set.
     */
    public void setUrl(String url) {
        this.url = url;
    }
    
    private String getFirstPage(){
        StringBuffer buf = new StringBuffer();
        
        if(pageNo>1) {
            buf.append("<a class='page' href=\"javascript:goPage(").append("1)\"> 第一页</a>");
        }
        else{
            buf.append("第一页");
        }
        
        return buf.toString();
    }
    
    private String getPreviousPage(){
        //int totalPage = getTotalPage();
        StringBuffer buf = new StringBuffer();
        
        if(pageNo>1) {
            buf.append("<a class='page' href=\"javascript:goPage(").append(pageNo-1).append(")\"> 上一页</a>");
        }
        else{
            buf.append("上一页");
        }
        
        return buf.toString();
    }
    
    private String getNextPage(){
        int totalPage = getTotalPage();
        StringBuffer buf = new StringBuffer();
        
        if(pageNo<totalPage) {
            buf.append("<a class='page' href=\"javascript:goPage(").append(pageNo+1).append(")\"> 下一页</a>");
        }
        else{
            buf.append("下一页");
        }
        
        return buf.toString();
    }
    
    private String getLastPage(){
        int totalPage = getTotalPage();
        StringBuffer buf = new StringBuffer();
        
        if(pageNo<totalPage) {
            buf.append("<a class='page' href=\"javascript:goPage(").append(totalPage).append(")\"> 最后页</a>");
        }
        else{
            buf.append("最后页");
        }
        
        return buf.toString();
    }
    
    private String getGoPage(){
        int totalPage = getTotalPage();
        StringBuffer buf = new StringBuffer();
        
        buf.append("<select name=\"_gotoPage\" onchange=\"goPage(this.value)\">");
        for(int i=1;i<=totalPage;i++){
            buf.append("<option value=\"").append(i).append("\"").append((i==pageNo)?" selected":"").append(">").append(i).append("</option>");
        }
        buf.append("</select>");
        
        return buf.toString();
    }
    
    /**
     * @return Returns the totalPage.
     */
    private int getTotalPage() {
        if(totalRecords%pageSize==0)
            return totalRecords/pageSize;
        return totalRecords/pageSize + 1;
    }
    /**
     * @return Returns the totalRecords.
     */
    public int getTotalRecords() {
        return totalRecords;
    }
    /**
     * @param totalRecords The totalRecords to set.
     */
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -