我的java天地

分页标签

1.pagination.tld

 1 <? xml version="1.0" encoding="UTF-8" ?>      
 2 <! DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
     
 3 < taglib >
  
 4      < tlib-version > 2.2.3 </ tlib-version >
     
 5      < jsp-version > 1.2 </ jsp-version >
     
 6      < short-name > tangs </ short-name >
     
 7      < uri > /tangs </ uri >
 
 8      < description > author xiongwei </ description >
   
 9      < display-name > "Tangs Tags" </ display-name >
     
10
          
11          < tag >
     
12              < name > pages </ name >
     
13              < tag-class > cn.com.sunrise.home.util.PageTag </ tag-class >
      
14              < body-content > empty </ body-content >
     
15              < attribute >
      
16                  < name > pageNo </ name >
     
17                  < required > true </ required >
     
18                  < rtexprvalue > true </ rtexprvalue >
     
19              </ attribute >
     
20              < attribute >
     
21                  < name > total </ name >
     
22                  < required > true </ required >
     
23                  < rtexprvalue > true </ rtexprvalue >
     
24              </ attribute >
    
25              < attribute >
     
26                  < name > permeterName </ name >
     
27                  < required > false </ required >
     
28                  < rtexprvalue > true </ rtexprvalue >
     
29              </ attribute >
    
30              < attribute >
     
31                  < name > includes </ name >
     
32                  < required > false </ required >
     
33                  < rtexprvalue > true </ rtexprvalue >
     
34              </ attribute >
   
35              < attribute >
     
36                  < name > styleClass </ name >
     
37                  < required > false </ required >
     
38                  < rtexprvalue > true </ rtexprvalue >
     
39              </ attribute >
     
40              < attribute >
     
41                  < name > theme </ name >
     
42                  < required > false </ required >
     
43                  < rtexprvalue > true </ rtexprvalue >
     
44              </ attribute >
    
45          </ tag >
     
46 </ taglib >
    
47

2.Pages.java
  1package cn.com.sunrise.home.util;   
  2
  
  3
import java.io.IOException;   
  4
import java.io.Writer;   
  5
import java.util.Map;   
  6
import java.util.logging.Level;   
  7
import java.util.logging.Logger;   
  8
  
  9
import javax.servlet.http.HttpServletRequest;   
 10
  
 11
import org.apache.struts2.StrutsStatics;   
 12
import org.apache.struts2.components.Component;   
 13
import org.apache.struts2.dispatcher.StrutsRequestWrapper;   
 14
  
 15
import com.opensymphony.xwork2.util.ValueStack;   
 16
  
 17
/**  
 18
 * 分页逻辑Bean  
 19
 * @author xiongwei
 20
 */  
 21
public class Pages extends Component {   
 22
       
 23
    private HttpServletRequest request;   
 24
       
 25
    private String pageNo;   
 26
    private String total;   
 27
    private String styleClass;   
 28
    private String theme;   
 29
    private String includes;   
 30
       
 31
    public String getTheme() {   
 32
        return theme;   
 33
    }   
 34
    public void setTheme(String theme) {   
 35
        this.theme = theme;   
 36
    }       
 37
    public String getIncludes() {   
 38
        return includes;   
 39
    }   
 40
    public void setIncludes(String includes) {   
 41
        this.includes = includes;   
 42
    }   
 43
    public String getStyleClass() {   
 44
        return styleClass;   
 45
    }   
 46
    public void setStyleClass(String styleClass) {   
 47
        this.styleClass = styleClass;   
 48
    }       
 49
    public String getPageNo() {   
 50
        return pageNo;   
 51
    }   
 52
    public void setPageNo(String pageNo) {   
 53
        this.pageNo = pageNo;   
 54
    }   
 55
    public String getTotal() {   
 56
        return total;   
 57
    }   
 58
    public void setTotal(String total) {   
 59
        this.total = total;   
 60
    }   
 61
       
 62
       
 63
    public Pages(ValueStack arg0, HttpServletRequest request) {   
 64
        super(arg0);   
 65
        this.request = request;   
 66
    }   
 67
  
 68
    @Override  
 69
    public boolean start(Writer writer) {   
 70
           
 71
        boolean result = super.start(writer);   
 72
        try {   
 73
            StringBuilder str = new StringBuilder();   
 74
            Map cont=this.getStack().getContext();   
 75
            StrutsRequestWrapper req=(StrutsRequestWrapper)cont.get(StrutsStatics.HTTP_REQUEST);   
 76
               
 77
            String url=(String)req.getAttribute("javax.servlet.forward.request_uri");   
 78
                          
 79
            //从ValueStack中取出数值   
 80
            Object obj=this.getStack().findValue(pageNo);               
 81
            pageNo = String.valueOf((Integer)obj);     
 82
            obj=this.getStack().findValue(total);   
 83
            total = String.valueOf((Integer)obj);     
 84
               
 85
            StringBuffer perUrl=new StringBuffer("");   
 86
            if(includes!=null){   
 87
                String[] perm=includes.split(",");   
 88                for(int i=0;i<perm
.length;i++){   
 89                    String permName
=perm[i];   
 90                    Object obje
=this.getStack().findValue(permName);   
 91                     String vType
=obje.getClass().toString();   
 92
                       
 93                    vType
=vType.substring(vType.lastIndexOf(".")+1,vType.length());                        
 94                    
perUrl.append("&"+permName);   
 95
                    if(vType.equals("String")){   
 96                        String tmp
= (String)this.getStack().findValue(permName);   
 97                        perUrl.append("
="+tmp);   
 98                    }else if(vType.equals("
Long")){   
 99                        Long tmp
= (Long)this.getStack().findValue(permName);   
100                        perUrl.append("
="+tmp);   
101                    }else if(vType.equals("
Float")){   
102                        Float tmp
= (Float)this.getStack().findValue(permName);   
103                        perUrl.append("
="+tmp);   
104                    }else if(vType.equals("
Integer")){   
105                        Integer tmp
= (Integer)this.getStack().findValue(permName);   
106                        perUrl.append("
="+tmp);   
107                    }else if(vType.equals("
Boolean")){   
108                        Boolean tmp
= (Boolean)this.getStack().findValue(permName);   
109                        perUrl.append("
="+tmp);   
110
                    }   
111
                }                  
112
            }   
113
               
114
               
115
            Integer cpageInt = Integer.valueOf(pageNo);   
116            str.append("
<span ");   
117            if (styleClass != null) 
{   
118                str.append(" class
='"+styleClass+"'>");   
119            
} else {   
120                str.append(">
");   
121
            }   
122
            //文本样式   
123
            if (theme == null || "text".equals(theme)) {   
124
                //当前页与总页数相等   
125
                if (pageNo.equals(total)) {   
126
                    //如果total = 1,则无需分页,显示“[第1页] [共1页]”   
127
                    if ("1".equals(total)) {   
128
                         
129
                    } else {   
130
                        //到达最后一页,显示“[首页] [上一页] [末页]” 
131                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
132                        str.append("<td><href='"+url+"?pageNo=1"+perUrl+"'><font class
='luntanbmy'>首页</font></a></td>");
133                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");  
134                        str.append("<td><href='"+url+"?pageNo=" + (cpageInt - 1)+perUrl+"'><font class
='luntanbmy'>上一页</font></a></td>"); 
135                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
136                        str.append("<td><href='#'><font class
='luntanbmy'>下一页</font></a></td>");
137                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
138                        str.append("<td><href='"+url+"?pageNo=" + total+perUrl+"'><font class
='luntanbmy'>最后一页</font></a></td>");
139                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
140                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span></td>
"); 
141                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
142                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span></td>
"); 
143
                    }   
144
                } else {   
145
                    //当前页与总页数不相同   
146
                    if ("1".equals(pageNo)) {   
147
                        //第一页,显示“[首页] [下一页] [末页]”   
148                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
149                        str.append("<td><href='"+url+"?pageNo=1"+perUrl+"'><font class
='luntanbmy'>首页</font></a></td>");
150                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");  
151                        str.append("<td><href='#'><font class
='luntanbmy'>上一页</font></a></td>"); 
152                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
153                        str.append("<td><href='"+url+"?pageNo=" + (cpageInt + 1) +perUrl+"'><font class
='luntanbmy'>下一页</font></a></td>");
154                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
155                        str.append("<td><href='"+url+"?pageNo=" + total+perUrl+"'><font class
='luntanbmy'>最后一页</font></a></td>");
156                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
157                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span></td>
"); 
158                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
159                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span></td>
"); 
160
                    } else {   
161
                        //不是第一页,显示“[首页] [上一页] [下一页] [末页]”   
162                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
163                        str.append("<td><href='"+url+"?pageNo=1"+perUrl+"'><font class
='luntanbmy'>首页</font></a></td>");
164                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");  
165                        str.append("<td><href='"+url+"?pageNo=" + (cpageInt - 1)+perUrl+"'><font class
='luntanbmy'>上一页</font></a></td>"); 
166                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
167                        str.append("<td><href='"+url+"?pageNo=" + (cpageInt + 1) +perUrl+"'><font class
='luntanbmy'>下一页</font></a></td>");
168                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
169                        str.append("<td><href='"+url+"?pageNo=" + total+perUrl+"'><font class
='luntanbmy'>最后一页</font></a></td>");
170                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
171                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span></td>
"); 
172                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>
");
173                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span></td>
"); 
174
                    }   
175
                }   
176
            } 
177            str.append("</span>
");   
178
               
179
            writer.write(str.toString());   
180
               
181
        } catch (IOException ex) {   
182
            Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);   
183
        }   
184
        return result;   
185
    }   
186
} 

3.PageTag.java
 1package cn.com.sunrise.home.util;   
 2
/*  
 3
 * To change this template, choose Tools | Templates  
 4
 * and open the template in the editor.  
 5 */
  
 6
  
 7
  
 8import
 com.opensymphony.xwork2.util.ValueStack;   
 9import
 javax.servlet.http.HttpServletRequest;   
10import
 javax.servlet.http.HttpServletResponse;   
11import
 org.apache.struts2.components.Component;   
12import
 org.apache.struts2.views.jsp.ComponentTagSupport;   
13
  
14
/**  
15
 * 分页标签  
16 * @author
 xiongwei  
17 */
  
18public class PageTag extends ComponentTagSupport 
{   
19
       
20    private
 String pageNo;   
21    private
 String total;   
22    private
 String styleClass;   
23    private
 String theme;   
24    private
 String includes;   
25
       
26    public void setTheme(String theme) 
{   
27        this.theme =
 theme;   
28    }
       
29    public void setStyleClass(String styleClass) 
{   
30        this.styleClass =
 styleClass;   
31    }
   
32    public void setPageNo(String pageNo) 
{   
33        this.pageNo =
 pageNo;   
34    }
   
35    public void setTotal(String total) 
{   
36        this.total =
 total;   
37    }
   
38    public String getIncludes() 
{   
39        return
 includes;   
40    }
   
41    public void setIncludes(String includes) 
{   
42        this.includes =
 includes;   
43    }
   
44
  
45
    @Override  
46    public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) 
{   
47        return new
 Pages(arg0, arg1);   
48    }
   
49
  
50    protected void populateParams() 
{   
51        super
.populateParams();   
52
           
53        Pages pages =
 (Pages)component;   
54
        pages.setPageNo(pageNo);   
55
        pages.setIncludes(includes);   
56
        pages.setTotal(total);   
57
        pages.setStyleClass(styleClass);   
58
        pages.setTheme(theme);   
59
  
60    }
   
61}
 

4.页面应用
<%@ taglib prefix="tangs" uri="/WEB-INF/pagination.tld"%>
<tangs:pages pageNo="pageNo" total="total"   theme="text"/>

5.action代码片段
 private Integer total;   
  private Integer pageNo=1;

 public String listAllHotNews(){
 pageConfig.setAllRecordNum(dao.recordCount());
  total = pageConfig.getAllPageNum();
  int start = pageConfig.getPerRecordNum()* (pageNo - 1);
  int size = pageConfig.getPerRecordNum();
  hotNewsList = dao.listAllNews(start,size);
  return SUCCESS;
 }


6.pageConfig.java
 1package cn.com.sunrise.home.credit.page;
 2
 3import cn.com.sunrise.home.util.Constraint;
 4
 5public class PageConfig {
 6    private int allRecordNum;// 总记录数
 7
 8    private int allPageNum;// 总页数
 9
10    private int perRecordNum =Constraint.PAGESIZE;// 每页显示数
11
12    private int currPageNO;// 当前页号
13
14    // 分页提供存取方法
15    public int getAllRecordNum() {
16        return allRecordNum;
17    }

18    //得到总页数,用下面的算法
19    public void setAllRecordNum(int allRecordNum) {
20        this.allRecordNum = allRecordNum;
21        if(allRecordNum % perRecordNum == 0){
22            this.allPageNum = allRecordNum / perRecordNum;
23        }
else{
24            this.allPageNum = allRecordNum / perRecordNum + 1;
25        }

26    }

27
28    public int getAllPageNum() {
29        return allPageNum;
30    }

31
32    public void setAllPageNum(int allPageNum) {
33        this.allPageNum = allPageNum;
34    }

35
36    public int getPerRecordNum() {
37        return perRecordNum;
38    }

39
40    public void setPerRecordNum(int perRecordNum) {
41        this.perRecordNum = perRecordNum;
42    }

43
44    public int getCurrPageNO() {
45        return currPageNO;
46    }

47
48    public void setCurrPageNO(int currPageNO) {
49        this.currPageNO = currPageNO;
50    }

51}

52
53

posted on 2009-09-25 16:29 tobyxiong 阅读(2498) 评论(0)  编辑  收藏 所属分类: java


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


网站导航:
 
<2009年9月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(3)

随笔分类(144)

随笔档案(157)

相册

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜