程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 分頁標簽

分頁標簽

編輯:關於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
3import java.io.IOException;
4import java.io.Writer;
5import java.util.Map;
6import java.util.logging.Level;
7import java.util.logging.Logger;
8
9import javax.servlet.http.HttpServletRequest;
10
11import org.apache.struts2.StrutsStatics;
12import org.apache.struts2.components.Component;
13import org.apache.struts2.dispatcher.StrutsRequestWrapper;
14
15import com.opensymphony.xwork2.util.ValueStack;
16
17/**
18 * 分頁邏輯Bean
19 * @author xiongwei
20 */
21public 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><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首頁</font></a></td>");
133                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
134                        str.append("<td><a 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><a href='#'><font class='luntanbmy'>下一頁</font></a></td>");
137                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
138                        str.append("<td><a 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><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首頁</font></a></td>");
150                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
151                        str.append("<td><a href='#'><font class='luntanbmy'>上一頁</font></a></td>");
152                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
153                        str.append("<td><a 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><a 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><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首頁</font></a></td>");
164                        str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
165                        str.append("<td><a 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><a 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><a 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved