如题,要实现一个分页功能,其次,要让分页标签“智能一点”,在分页时能自动带上url后面的参数

 1     <tag>
 2         <description>分页标签</description>  
 3         <name>paging</name>  
 4         <tag-class>com.micromis.util.tag.PagingTag</tag-class>  
 5         <body-content>empty</body-content>  
 6         <attribute>  
 7             <description>跳转链接</description>  
 8             <name>href</name>  
 9             <required>true</required>  
10             <rtexprvalue>true</rtexprvalue>  
11         </attribute>  
12         <attribute>  
13             <description>当前页</description>  
14             <name>curr</name>  
15             <required>true</required>  
16             <rtexprvalue>true</rtexprvalue>  
17         </attribute>  
18         <attribute>  
19             <description>每页条数</description>  
20             <name>size</name>  
21             <required>true</required>  
22             <rtexprvalue>true</rtexprvalue>  
23         </attribute>  
24         <attribute>  
25             <description>总页数</description>  
26             <name>total</name>  
27             <required>true</required>  
28             <rtexprvalue>true</rtexprvalue>  
29         </attribute>  
30         <attribute>  
31             <description>curr parameter name</description>  
32             <name>cparam</name>  
33             <required>false</required>  
34             <rtexprvalue>false</rtexprvalue>  
35         </attribute>  
36         <attribute>  
37             <description>page size parameter name</description>  
38             <name>sparam</name>  
39             <required>false</required>  
40             <rtexprvalue>false</rtexprvalue>  
41         </attribute>  
42         <dynamic-attributes>false</dynamic-attributes>  
43     </tag> 

 

标签类实现:

  1 package com.micromis.util.tag;
  2 
  3 import java.io.IOException;  
  4 import java.util.Iterator;
  5 import java.util.Map;
  6 import java.util.Map.Entry;
  7 import java.util.Set;
  8 
  9 import javax.servlet.http.HttpServletRequest;
 10 import javax.servlet.jsp.JspException;  
 11 import javax.servlet.jsp.JspWriter;  
 12 import javax.servlet.jsp.PageContext;
 13 import javax.servlet.jsp.tagext.SimpleTagSupport;  
 14   
 15 
 16 import org.apache.commons.lang.StringUtils;  
 17   
 18 /**
 19  * @ClassName: PagingTag
 20  * @Description: 分页标签类
 21  * @author lijie
 22  * @date 2015年3月6日 下午1:40:49
 23  */
 24 public class PagingTag extends SimpleTagSupport {  
 25       
 26     /** 
 27     * @Fields href : 分页跳转链接
 28     */ 
 29     private String href;  
 30       
 31     /** 
 32     * @Fields cparam : 当前页参数名,默认currentPage
 33     */ 
 34     private String cparam;  
 35     /** 
 36     * @Fields sparam : 每页条数的参数名,默认pageSize
 37     */ 
 38     private String sparam;  
 39   
 40     /** 
 41     * @Fields curr : 当前页
 42     */ 
 43     private int curr;
 44       
 45     /** 
 46     * @Fields size : 每页条数  
 47     */ 
 48     private int size;
 49       
 50     /** 
 51     * @Fields total : 总条数 
 52     */ 
 53     private int total;
 54       
 55     @Override  
 56     public void doTag() throws JspException, IOException {  
 57         JspWriter out = getJspContext().getOut();  
 58         
 59         PageContext pageContext=(PageContext)getJspContext();
 60         
 61         //下面获取url参数
 62         HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
 63         
 64         Map<String, String[]> map=request.getParameterMap();
 65         
 66         StringBuffer paramString=new StringBuffer();
 67         Set<Entry<String, String[]>> set3=map.entrySet();   //此处是entrySet()方法
 68         Iterator<Entry<String, String[]>> it3=set3.iterator();
 69         while(it3.hasNext()){
 70             Map.Entry<String, String[]> en=(Map.Entry<String, String[]>)it3.next();
 71             String key=en.getKey();
 72             String[] value=en.getValue();
 73             if(key!=null&&value!=null){
 74                 if("get".equalsIgnoreCase(request.getMethod())){ //根据get或post方法分别处理乱码问题
 75                     for(String s:value){
 76                         paramString.append(key+"="+new String(s.getBytes("ISO8859-1"),"utf-8")+"&"); //此处从ISO8859-1转是因为tomcat默认URI编码是ISO8859-1,防止中文乱码
 77                     }
 78                 }else{
 79                     for(String s:value){
 80                         paramString.append(key+"="+s+"&");
 81                     }
 82                 }
 83             }
 84         }
 85         
 86         String urlparam=paramString.toString();
 87         
 88         if(StringUtils.isEmpty(href)){
 89         }  
 90         
 91         if(StringUtils.isEmpty(cparam)) {  
 92             cparam = "currentPage";  
 93         }  
 94         if(StringUtils.isEmpty(sparam)) {  
 95             sparam = "pageSize";  
 96         }  
 97           
 98         if(!href.endsWith("?") && !href.endsWith("&")) {  
 99             if(href.indexOf("?") == -1) {  
100                 href = href + "?"+urlparam;  
101             } else {  
102                 href = href + "&"+urlparam;  
103             }  
104         }  
105           
106         //总页数
107         int pageTotal=1;
108         if(total<=0){
109             pageTotal=1;
110         }else{
111             pageTotal=total/size;
112             if(total%size>0){
113                 pageTotal=pageTotal+1;
114             }
115         }
116         
117         if (curr <= 0) {  
118             curr = 1;  
119         } else if (curr > pageTotal) {  
120             curr = pageTotal;  
121         }  
122         
123         out.append("<div class=\"pagingClass\">");
124         out.append("<p>");
125         out.append("(共"+total+"项记录,分"+pageTotal+"页,当前为第"+curr+"页)");
126         if(curr==1){
127             out.append("<label title=\"首页\"><<</label>");
128             out.append("<label title=\"上一页\"><</label>");
129         }else{
130             href(out, href, 1, "<<","首页");  
131             href(out, href, curr - 1, "<","上一页");
132         }
133         
134         initNum(pageTotal, out);
135         
136         if(curr==pageTotal){
137             out.append("<label title=\"下一页\">></label>");
138             out.append("<label title=\"末页\">>></label>");
139         }else{
140              href(out, href, curr +1, ">","下一页");
141              href(out, href, pageTotal, ">>","末页");  
142         }
143         
144         out.append("</p>");
145         out.append("</div>");
146         
147         super.doTag();  
148     }  
149       
150     private void href(JspWriter out, String href, int curr, String text,String title) throws IOException { 
151         if(this.curr==curr){
152             out.append("<label title=\""+title+"\">"+text+"</label>");
153         }else{
154             out.append("<a href=\"").append(href).append(cparam).append("=").append("" + curr).append("&").append(sparam).append("=").append("" + size).append(" \" title=\""+title+"\">").append(text).append("</a>");
155         }
156     }  
157   
158     private void initNum(int pageTotal,JspWriter out) throws IOException{
159         //此处默认最多显示5个页码
160         int showPageNum=5;
161         int showNum;
162         if(pageTotal<=showPageNum){
163             for(int i=1;i<=pageTotal;i++){
164                 showNum=i;
165                 href(out,href,showNum,showNum+"","第"+showNum+"页");
166             }
167         }else{
168             if(curr>(pageTotal-showPageNum)){
169                 for(int i=1;i<=showPageNum;i++){
170                     showNum=pageTotal-showPageNum+i;
171                     href(out,href,showNum,showNum+"","第"+showNum+"页");
172                 }
173             }else{
174                 for(int i=1;i<=showPageNum;i++){
175                     showNum=curr+i-1;
176                     href(out,href,showNum,showNum+"","第"+showNum+"页");
177                 }
178             }
179         }
180     }
181     
182     /**
183      * @Title: getCurr
184      * @Description: 获取当前页数
185      * @return
186      * @author lijie
187      * @throws
188      */
189     public int getCurr() {  
190         return curr;  
191     }  
192   
193     /**
194      * @Title: setCurr
195      * @Description: 设置当前页数
196      * @param curr
197      * @author lijie
198      * @throws
199      */
200     public void setCurr(int curr) {  
201         this.curr = curr;  
202     }  
203   
204     /**
205      * @Title: getTotal
206      * @Description: 获取总条数
207      * @return
208      * @author lijie
209      * @throws
210      */
211     public int getTotal() {  
212         return total;  
213     }  
214   
215     /**
216      * @Title: setTotal
217      * @Description: 设置总条数
218      * @param total
219      * @author lijie
220      * @throws
221      */
222     public void setTotal(int total) {  
223         this.total = total;  
224     }  
225   
226     /**
227      * @Title: getHref
228      * @Description: 获取分页跳转链接
229      * @return
230      * @author lijie
231      * @throws
232      */
233     public String getHref() {  
234         return href;  
235     }  
236   
237     /**
238      * @Title: setHref
239      * @Description: 设置分页跳转链接
240      * @param href
241      * @author lijie
242      * @throws
243      */
244     public void setHref(String href) {  
245         this.href = href;  
246     }  
247   
248     /**
249      * @Title: getCparam
250      * @Description: 获取当前页参数名,默认currentPage
251      * @return
252      * @author lijie
253      * @throws
254      */
255     public String getCparam() {  
256         return cparam;  
257     }  
258   
259     /**
260      * @Title: setCparam
261      * @Description: 设置当前页参数名
262      * @param cparam
263      * @author lijie
264      * @throws
265      */
266     public void setCparam(String cparam) {  
267         this.cparam = cparam;  
268     }  
269   
270     /**
271      * @Title: getSparam
272      * @Description: 获取每页条数的参数名,默认pageSize
273      * @return
274      * @author lijie
275      * @throws
276      */
277     public String getSparam() {  
278         return sparam;  
279     }  
280   
281     /**
282      * @Title: setSparam
283      * @Description: 设置每页条数的参数名
284      * @param sparam
285      * @author lijie
286      * @throws
287      */
288     public void setSparam(String sparam) {  
289         this.sparam = sparam;  
290     }  
291   
292     /**
293      * @Title: getSize
294      * @Description: 获取每页条数
295      * @return
296      * @author lijie
297      * @throws
298      */
299     public int getSize() {  
300         return size;  
301     }  
302   
303     
304     /**
305      * @Title: setSize
306      * @Description: 设置每页条数
307      * @param size
308      * @author lijie
309      * @throws
310      */
311     public void setSize(int size) {  
312         this.size = size;  
313     }  
314   
315 }  

 

 

 

分页的css样式:

1 .pagingClass *{font-size:12px;font-family:"微软雅黑";float: right;}
2 .pagingClass{text-align:left;margin:0 0 4px 0;height:50px;width:600px;float:right}
3 .pagingClass a{text-align:center;cursor:pointer;display:block;width:15px;float:left;height:15px;padding:5px;color:#000071;font-weight:700;margin:0 2px 0 2px;border:1px #21385b solid;background-color:#ecf5ff;text-decoration:none;font-family:"Arial";}
4 .pagingClass label{text-align:center;display:block;width:15px;float:left;height:15px;padding:5px;color:#000071;font-weight:700;margin:0 2px 0 2px;text-decoration:none;font-family:"Arial";border:1px #21385b solid;}
5 .pagingClass a.curp{border:1px #f60 solid;background-color:#fff;}
6 .pagingClass a:hover,.pagingClass a:active{background-color:#21385b;color:#fff;}

 

页面调用:

<page:paging size="10" total="${count}" curr="${curr}" href="${contextPath}/policyCompany/queryWhiteList"/>

 

效果:

在不修改任何代码的情况下:

不带条件查询时,只是在连接后加了分页参数

在 公司名称出输入内容,点击查询