本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0

/**
 *
 */
package com.scitel.gdnumcommon.ctag;

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

import com.scitel.gdnumcommon.entity.Pagination;

/**
 *
 *
 * <p>Description: 分页标签类</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: 深圳科讯数码有限公司</p>
 *
 * @author 
 * @version 1.0 时间 Mar 6, 2006
 */
public class PaginationTag extends TagSupport {
 /**
  * 唯一ID
  */
 private static final long serialVersionUID = 1673491971683216709L;
    /**
     * 链接路径
     */
 private String path = null;
 /**
  * 分页类保存在request中的名称,与类名相同
  */
 private String name = null;
 /**
  * 所带参数名称(page)
  */
 private String parameter = null;
 /**
  *表单名称
  */
 private String formName = null;
 
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @param path The path to set.
  */
 public void setPath(String path) {
  this.path = path;
 }
 
 /**
  * @param parameter The parameter to set.
  */
 public void setParameter(String parameter) {
  this.parameter = parameter;
 }
 
 /**
  * @param formName The formName to set.
  */
 public void setFormName(String formName) {
  this.formName = formName;
 }
 
 /* (non-Javadoc)
  * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
  */
 public int doStartTag() throws JspException {
  // TODO Auto-generated method stub
  Pagination pagination = null;

  if(this.path == null){
   throw new NullPointerException("path属性不能为空");
  }
  
  if(this.name == null){
   this.name = "pagination";
  }
  
  if(this.parameter == null){
   this.parameter = "page";
  }
  
  pagination = (Pagination)pageContext.getRequest().getAttribute(name);
  
  if(pagination == null) {
   throw new NullPointerException(name + "在request中为空!");
  }
  
  try{
   JspWriter out = pageContext.getOut();

   String contextPath = encodeURL(this.path, this.parameter);
   if(this.formName != null && this.formName.length() > 0){
    if(pagination.isHasPreviousPage()){
     out.println("<a class=\"pagination\" href=\"" + contextPath + "1\" onclick=\"paginationSubmit('1');return false;\" title=\"第一页\">第一页</a>");
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getPreviousPage() + "\" onclick=\"paginationSubmit('" + pagination.getPreviousPage() + "');return false;\" title=\"上一页\">上一页</a>");
    }else{
     out.println("<span title=\"第一页\">第一页</span>");
     out.println("<span title=\"上一页\">上一页</span>");
    }
    
    if(pagination.isHasNextPage()){
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getNextPage() + "\" onclick=\"paginationSubmit('" + pagination.getNextPage() + "');return false;\"  title=\"下一页\">下一页</a>");
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getTotalPage() + "\" onclick=\"paginationSubmit('" + pagination.getTotalPage() + "');return false;\" title=\"最后一页\">最后一页</a>");
    }else{
     out.println("<span title=\"下一页\">下一页</span>");
     out.println("<span title=\"最后一页\">最后一页</span>");
    }
   }else{
    if(pagination.isHasPreviousPage()){
     out.println("<a class=\"pagination\" href=\"" + contextPath + "1\" title=\"第一页\">第一页</a>");
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getPreviousPage() + "\" title=\"上一页\">上一页</a>");
    }else{
     out.println("<span title=\"第一页\">第一页</span>");
     out.println("<span title=\"上一页\">上一页</span>");
    }
    
    if(pagination.isHasNextPage()){
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getNextPage() + "\" title=\"下一页\">下一页</a>");
     out.println("<a class=\"pagination\" href=\"" + contextPath + pagination.getTotalPage() + "\" title=\"最后一页\">最后一页</a>");
    }else{
     out.println("<span title=\"下一页\">下一页</span>");
     out.println("<span title=\"最后一页\">最后一页</span>");
    }
   }
   
   out.println("&nbsp;");
   out.println("共有" + pagination.getTotalCount() + "条");
   out.print("&nbsp;");
   out.println("每页" + pagination.getCount() + "条");
   out.print("&nbsp;");
   out.println("共" + pagination.getTotalPage() + "页");
   out.print("&nbsp;");
   out.println("本页是第" + pagination.getCurrPage() + "页");
   out.println("&nbsp;");
   out.println("跳转到第<input type=\"text\" id=\"" + this.parameter + "\" class=\"input1\" size=\"3\" maxlength=\"5\">页");
   out.println("&nbsp;");
   out.println("<button onclick=\"paginationGoto();\" class=\"buttonbg\">跳转</button>");
   out.println("<br />");
   /*创建CSS内容*/
   createCSS(out);
   if(this.formName != null && this.formName.length() > 0) {
    /*创建js内容*/
    createJS(out, contextPath, pagination.getTotalPage());
   }else{
    createNoFormJS(out, this.path, pagination.getTotalPage());
   }
   out.println();
  }catch(Exception e){
   throw new JspException(e);
  }
  
  return SKIP_BODY;
 }
 
 /* (non-Javadoc)
  * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
  */
 public int doEndTag() throws JspException {
  // TODO Auto-generated method stub
  return EVAL_PAGE;
 }
 
 /**
  * 改变URL使它能访问到action或servlet中去
  * @param href 从页面传来的href
  * @param param
  * @param queryStr 查询内容
  * @return
  * @author 
  */
 private final String encodeURL(String href, String param){
  StringBuffer buffer = new StringBuffer(100);
  
  HttpServletRequest request =
   (HttpServletRequest)this.pageContext.getRequest();
  buffer.append(request.getContextPath() + "/" + href);

  // 判断是否已经带有?号
  int question = href.indexOf("?");
  if(question > 0 ){
   buffer.append("&" + param + "=");
  }else{
   buffer.append("?" + param + "=");
  }
  
  return buffer.toString();
 }
 
 /**
  * 创建js内容如果提交form就用js提交
  * @param out
  * @param contextPath
  * @throws Exception
  * @author 
  */
 private final void createJS(JspWriter out, String contextPath, int totalPage) throws Exception {
  out.println("<script language=\"javascript\">");
  out.println("function paginationSubmit(pageNum_) { ");
  out.println("  document." + formName + ".action=\'" + contextPath + "\' + pageNum_ + \'\';");
  out.println("  document." + formName + ".submit();");
  out.println("}");
  /*跳转内容*/
  HttpServletRequest request =
   (HttpServletRequest)this.pageContext.getRequest();
  String contPath = request.getContextPath() + "/" + this.path;
  out.println("function paginationGoto() { ");
  out.println("  if(!controlNotNull(document.all." + this.parameter + ", '跳转页面')) {");
  out.println("    return;");
  out.println("  }");
  out.println("  ");
  out.println("  if(!controlIsInt(document.all." + this.parameter + ", '跳转页面')) {");
  out.println("    return;");
  out.println("  }");
  out.println("  ");
  out.println("  pageNum_ = document.all." + this.parameter + ".value");
  out.println("  if(pageNum_ > 0 && pageNum_ <= " + totalPage + ") {");
  out.println("    document." + formName + ".action=\'" + contPath + "&" + this.parameter + "=\' + pageNum_ + \'\';");
  out.println("    document." + formName + ".submit();");
  out.println("  } else { ");
  out.println("    alert(\"您输入的页数超出范围\");");
  out.println("  }");
  out.println("}");
  out.println("</script>");
  out.println();
 }
 
 /**
  * 创建没有form时的js
  * @param out
  * @param contextPath
  * @throws Exception
  * @author 
  */
 private final void createNoFormJS(JspWriter out, String href, int totalPage) throws Exception {
  HttpServletRequest request =
   (HttpServletRequest)this.pageContext.getRequest();
  String contPath = request.getContextPath() + "/" + href;

  out.println("<script language=\"javascript\">");
  out.println("function paginationGoto() { ");
  out.println("  if(!controlNotNull(document.all." + this.parameter + ", '跳转页面')) {");
  out.println("    return;");
  out.println("  }");
  out.println("  ");
  out.println("  if(!controlIsInt(document.all." + this.parameter + ", '跳转页面')) {");
  out.println("    return;");
  out.println("  }");
  out.println("  ");
  out.println("  pageNum_ = document.all." + this.parameter + ".value");
  out.println("  if(pageNum_ > 0 && pageNum_ <= " + totalPage + ") {");
  out.println("    document.location.href=\'" + contPath + "&" + this.parameter + "=\' + pageNum_ + \'\';");
  out.println("  } else { ");
  out.println("    alert(\"您输入的页数超出范围\");");
  out.println("  }");
  out.println("}");
  out.println("</script>");
  out.println();
 }
 
 /**
  * 创建css内容
  * @param out
  * @throws Exception
  * @author 

  */
 private final void createCSS(JspWriter out)throws Exception {
  out.println("<style type=\"text/css\">");
  out.println("a.pagination:visited { text-decoration: underline; color: #808080; } ");//#CCCCFF #c0c0c0
  out.println("a.pagination:link { text-decoration: underline; color: blue; } ");
  out.println("a.pagination:hover { text-decoration: none; color: #009933; } ");
  out.println("a.pagination:active { text-decoration: none; color: orangered; } ");
  out.println("</style>");
 }
}
========================================
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>page</short-name>
 <description>
 @author
 pagination page 1-10
 </description>

 <tag>
  <name>pagination</name>
  <tag-class>com.scitel.gdnumcommon.ctag.PaginationTag</tag-class>
  <body-content>empty</body-content>

  <attribute>
   <name>path</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>

  <attribute>
   <name>name</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>

  <attribute>
   <name>parameter</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
  
  <attribute>
   <name>formName</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>

 </tag>
</taglib>
=============================
页面使用代码
  <Td align="right" colspan="10">
  <page:pagination path="wfProcAction.do?method=queryWorkFlow" name="pagination" parameter="page" formName="form1" />
  </Td>
===============================================
Action 类中使用代码
     String page = request.getParameter("page");
     Pagination pagination = new Pagination();
     pagination.setTotalCount(didWf.size());
    
                 if (page != null && page.length() > 0) {
          pagination.setPage(Integer.parseInt(page));
     }else {
          pagination.setPage(1);
     }
                 int page_=pagination.getPage();
                 int row_start=(page_*pagination.getCount())-pagination.getCount();
                 int row_end=page_*pagination.getCount();
                 if(row_end>didWf.size()) {
                  row_end=didWf.size();
                 }
                 List datalist=didWf.subList(row_start,row_end);
                 request.setAttribute("wkList",datalist);
                
                 request.setAttribute("pagination", pagination);
===============================

posted on 2007-01-08 15:07 有猫相伴的日子 阅读(1257) 评论(1)  编辑  收藏 所属分类: j2ee

评论:
# re: 一个自定义分页标签 2007-06-10 19:58 | kbfox
似乎不太完整啊  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网