JAVA自定义分页标签类

系统 1973 0
 

1,标签的实现类NumenTag.java

 

import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;

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

/** */ /**
 * 自定义的分页标签
 * 
 * 
@author  马万林
 * 
 
*/

public   class  NumenTag  extends  TagSupport  {

    
private  String url  =   null ;

    
private  Result result;

    
private  String page  =   null ;

    
private  String[] p;

    
private  Map kv  =   new  HashMap();

    
private  List key  =   null ;

    
/** */ /**
     * 分离参数
     * 
@param  params
     
*/

    
public   void  setParams(String params)  {
        key 
=   new  ArrayList();
        p 
=  params.split( " ; " );
        
for  ( int  i  =   0 ; p.length > 1 && <  p.length; i ++ {
            kv.put(p[i].split(
" : " )[ 0 ], p[i].split( " : " )[ 1 ]);
            key.add(p[i].split(
" : " )[ 0 ]);
        }

    }


    
/** */ /**
     * 分页的结果
     * 
@param  result
     
*/

    
public   void  setResult(String result)  {
        
this .result  =  (Result) pageContext.getRequest().getAttribute( " result " );
        
this .page  =  (String) pageContext.getRequest().getAttribute( " page " );
    }


    
public   void  setUrl(String url)  {
        
this .url  =  url;
    }


    
public   int  doStartTag()  throws  JspException  {
        StringBuffer sb 
=   null ;
        
if  (result  !=   null   &&  result.getContent()  !=   null
                
&&  result.getContent().size()  >   0 {
            sb 
=   new  StringBuffer( " 分页:  " );
            
if  (result.getPage().getHasPrePage()) {
                sb.append(
" <a href=' " ).append(url);
                
if  (url.indexOf( " ? " ==   - 1 )
                    sb.append(
" ?page=1 " );
                
else
                    sb.append(
" &page=1 " );
                linkParams(sb);
                sb.append(
" '> " ).append( " 首页 " ).append( " </a> " ).append( "  |  " );
                sb.append(
" <a href=' " ).append(url);
                
if  (url.indexOf( " ? " ==   - 1 )
                    sb.append(
" ?page= " ).append(
                            result.getPage().getPreviousPage());
                
else
                    sb.append(
" &page= " ).append(
                            result.getPage().getPreviousPage());
                linkParams(sb);
                sb.append(
" '> " ).append( " 上页 " ).append( " </a> " ).append( "  |  " );
            }

            
if  (result.getPage().getHasNextPage())  {
                sb.append(
" <a href=' " ).append(url);
                
if  (url.indexOf( " ? " ==   - 1 )
                    sb.append(
" ?page= " ).append(result.getPage().getNextPage());
                
else
                    sb.append(
" &page= " ).append(result.getPage().getNextPage());
                linkParams(sb);
                sb.append(
" '> " ).append( " 下页 " ).append( " </a> " ).append( "  |  " );
                sb.append(
" <a href=' " ).append(url);
                
if  (url.indexOf( " ? " ==   - 1 )
                    sb.append(
" ?page= " ).append(result.getPage().getTotalPage());
                
else
                    sb.append(
" &page= " ).append(result.getPage().getTotalPage());
                linkParams(sb);
                sb.append(
" '> " ).append( " 尾页 " ).append( " </a> " );
            }

            sb
                    .append(
"  跳转到:<select name='goToPage' onchange='javascript:goPage(this.value)'> " );
            
int  s  =  result.getPage().getTotalCount();
            
for  ( int  i  =   1 ; i  <=  s; i ++ {
                sb.append(
" <option value= " ).append(i);
                
if  (Integer.parseInt(page)  ==  i)
                    sb.append(
"  selected " );
                sb.append(
" >第 " ).append(i).append( " 页</option> " );
            }

            sb.append(
" </select> " );
            sb.append(
" <script language=javascript> " );
            sb.append(
" function goPage(page){ " ).append( " window.location=' " )
                    .append(url);
            
if  (url.indexOf( " ? " ==   - 1 )
                sb.append(
" ?page='+ " ).append( " page " );
            
else
                sb.append(
" &page='+ " ).append( " page " );
            sb.append(
" +' " );
            linkParams(sb);
            sb.append(
" '; " );
            sb.append(
" } " );
            sb.append(
" </script> " );
        }

        
try   {
            
if  (sb  !=   null {
                JspWriter out 
=  pageContext.getOut();
                out.print(sb);
            }

        }
  catch  (Exception e)  {
            
throw   new  JspException(e);
        }

        
return  EVAL_PAGE;
    }

    
    
/** */ /**
     * 链接参数处理
     * 
@param  sb
     
*/

    
private   void  linkParams(StringBuffer sb)  {
        
for  ( int  i  =   0 ; i  <  key.size(); i ++ {
            sb.append(
" & " ).append(key.get(i)).append( " = " ).append(
                    kv.get(key.get(i)));
        }

    }


}

 

2,标签定义文件numentag.tld

 

<? 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 > numen </ short-name

JAVA自定义分页标签类


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论