struts2 分页

来源:互联网 发布:js饼状图显示百分比 编辑:程序博客网 时间:2024/05/17 03:23
要实现分页,基本流程为JSP->PageTag->PageBean->Action,当然也可以反过来说。 
1. 首先从Action说起吧,Action需要几个参数,其中pageNo(当前页),total(总共多少条记录)是必需的,查询的条件参数则是可选的。基本的Action代码如下: 

Java代码  收藏代码
  1. public class HelloWorld extends ActionSupport {  
  2.   
  3.     private static final long serialVersionUID = 7046981255032101657L;  
  4.   
  5.     // 总共页数  
  6.     private int total = 40;  
  7.     // 默认当前页为第一页  
  8.     private int pageNo = 1;  
  9.     // 下面为条件参数  
  10.     private String test = "test";  
  11.     private long test1 = 5L;  
  12.     private float test2 = 9.8F;  
  13.     private int test3 = 8;  
  14.   
  15.     public String execute() {  
  16.         return SUCCESS;  
  17.     }  
  18.   
  19.     public int getTotal() {  
  20.         return total;  
  21.     }  
  22.   
  23.     public void setTotal(int total) {  
  24.         this.total = total;  
  25.     }  
  26.   
  27.     public int getPageNo() {  
  28.         return pageNo;  
  29.     }  
  30.   
  31.     public void setPageNo(int pageNo) {  
  32.         this.pageNo = pageNo;  
  33.     }  
  34.   
  35.     public String getTest() {  
  36.         return test;  
  37.     }  
  38.   
  39.     public void setTest(String test) {  
  40.         this.test = test;  
  41.     }  
  42.   
  43.     public long getTest1() {  
  44.         return test1;  
  45.     }  
  46.   
  47.     public void setTest1(long test1) {  
  48.         this.test1 = test1;  
  49.     }  
  50.   
  51.     public float getTest2() {  
  52.         return test2;  
  53.     }  
  54.   
  55.     public void setTest2(float test2) {  
  56.         this.test2 = test2;  
  57.     }  
  58.   
  59.     public int getTest3() {  
  60.         return test3;  
  61.     }  
  62.   
  63.     public void setTest3(int test3) {  
  64.         this.test3 = test3;  
  65.     }  
  66.   
  67. }  


需要说明的,上面的一些参数如pageNo,total对于每个需要分页的Action来说都是固定的,所以可以提取到一个抽象的类中。只要JSP页面的参数名与Action的变量名对应起来,ONGL就会把相应的值附给这些变量。如下: 

Html代码  收藏代码
  1. <p:pages pageNo="pageNo" total="total"  
  2.             includes="test,test1,test2,test3"/>  


其中includes包含的就是查询的条件参数名,它们与Action查询变量相对应。通过上面的JSP页面,可以看出用到了一个tag,不过这个tag tld文件很简单,page.tld: 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  3.                         "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">  
  4. <taglib>       
  5.     <tlib-version>2.2.3</tlib-version>       
  6.     <jsp-version>1.2</jsp-version>       
  7.     <short-name>p</short-name>       
  8.     <uri>/p</uri>       
  9.     <display-name>"pages Tags"</display-name>       
  10.             
  11.         <tag>       
  12.             <name>pages</name>       
  13.             <tag-class>com.page.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>includes</name>       
  27.                 <required>false</required>       
  28.                 <rtexprvalue>true</rtexprvalue>       
  29.             </attribute>  
  30.         </tag>       
  31. </taglib>  


这个tag对应的tag类也很简单: 

Java代码  收藏代码
  1. public class PageTag extends ComponentTagSupport {  
  2.       
  3.     private static final long serialVersionUID = 7242423813230124088L;  
  4.     //这里传递的参数需要用字符串的形式  
  5.     private String pageNo;  
  6.     private String total;  
  7.     private String includes;  
  8.   
  9.     public void setPageNo(String pageNo) {  
  10.         this.pageNo = pageNo;  
  11.     }  
  12.   
  13.     public void setTotal(String total) {  
  14.         this.total = total;  
  15.     }  
  16.   
  17.     public String getIncludes() {  
  18.         return includes;  
  19.     }  
  20.   
  21.     public void setIncludes(String includes) {  
  22.         this.includes = includes;  
  23.     }  
  24.   
  25.     @Override  
  26.     public Component getBean(ValueStack arg0, HttpServletRequest arg1,  
  27.             HttpServletResponse arg2) {  
  28.         return new Pages(arg0);  
  29.     }  
  30.   
  31.     protected void populateParams() {  
  32.         super.populateParams();  
  33.   
  34.         Pages pages = (Pages) component;  
  35.         pages.setPageNo(pageNo);  
  36.         pages.setIncludes(includes);  
  37.         pages.setTotal(total);  
  38.   
  39.     }  
  40. }  


需要注意的是ongl对变量的获取方法findValue的形式并不多,只提供对String的支持,因此需要用字符串变量,这方面的确不是很完善。这个tag会把相应的值交给PageBean进行分页显示的处理。因此最重要的东西其实是PageBean类: 

Java代码  收藏代码
  1. public class Pages extends Component {  
  2.   
  3.     private String pageNo;  
  4.     private String total;  
  5.     private String includes;  
  6.   
  7.     public String getIncludes() {  
  8.         return includes;  
  9.     }  
  10.   
  11.     public void setIncludes(String includes) {  
  12.         this.includes = includes;  
  13.     }  
  14.   
  15.     public String getPageNo() {  
  16.         return pageNo;  
  17.     }  
  18.   
  19.     public void setPageNo(String pageNo) {  
  20.         this.pageNo = pageNo;  
  21.     }  
  22.   
  23.     public String getTotal() {  
  24.         return total;  
  25.     }  
  26.   
  27.     public void setTotal(String total) {  
  28.         this.total = total;  
  29.     }  
  30.   
  31.     public Pages(ValueStack arg0) {  
  32.         super(arg0);  
  33.     }  
  34.   
  35.     @Override  
  36.     public boolean start(Writer writer) {  
  37.   
  38.         boolean result = super.start(writer);  
  39.         StringBuilder str = new StringBuilder();  
  40.         Map<String, Object> cont = stack.getContext();  
  41.         StrutsRequestWrapper req = (StrutsRequestWrapper) cont  
  42.                 .get(StrutsStatics.HTTP_REQUEST);  
  43.   
  44.         String url = (String) req  
  45.                 .getAttribute("javax.servlet.forward.request_uri");  
  46.   
  47.         // 从ValueStack中取出数值  
  48.         Object obj = stack.findValue(pageNo);  
  49.         pageNo = String.valueOf(obj);  
  50.         obj = stack.findValue(total);  
  51.         total = String.valueOf(obj);  
  52.   
  53.         StringBuilder perUrl = new StringBuilder("");  
  54.         if (includes != null && includes.trim().length() > 0) {  
  55.             String[] perm = includes.split(",");  
  56.             for (int i = 0; i < perm.length; i++) {  
  57.                 String permName = perm[i];  
  58.                 Object obje = stack.findValue(permName);  
  59.   
  60.                 perUrl.append("&");  
  61.                 perUrl.append(permName);  
  62.                 perUrl.append("=");  
  63.                 perUrl.append(obje);  
  64.             }  
  65.         }  
  66.   
  67.         //用于计算的当前页整数形式  
  68.         int cpageInt = Integer.valueOf(pageNo);  
  69.         str.append("<div class='pagination'>");  
  70.         Integer totalInt = Integer.valueOf(total);  
  71.   
  72.         // 如果只有一页,则无需分页  
  73.   
  74.         if (totalInt == 1) {  
  75.             str.append("<span class='current'>1</span> ");  
  76.         } else {  
  77.   
  78.             // 显示上一页与第一页  
  79.             if (cpageInt == 1) {  
  80.                 str.append("<span class='disabled'><< 上一页</span>");  
  81.                 str.append("<span class='current'>1</span>");  
  82.             } else {  
  83.                 str.append("<a href='");  
  84.                 str.append(url);  
  85.                 str.append("?pageNo=");  
  86.                 str.append(cpageInt - 1);  
  87.                 str.append(perUrl);  
  88.                 str.append("'>« 上一页</a>");  
  89.                   
  90.                 str.append("<a href='");  
  91.                 str.append(url);  
  92.                 str.append("?pageNo=1");  
  93.                 str.append(perUrl);  
  94.                 str.append("'>1</a>");  
  95.             }  
  96.   
  97.             // 当前页超过5时第一页后面加点,因为中间相隔了第二页  
  98.             if (cpageInt - 4 > 1)  
  99.                 str.append("<span class='gap'>...</span>");  
  100.   
  101.             // v,v1分别代表中间页数的最小值和最大值,3表示显示当前页的前后三页  
  102.             int v = (cpageInt - 3) > 1 ? (cpageInt - 3) : 2;  
  103.             int v1 = (cpageInt + 3) < totalInt ? (cpageInt + 3) : totalInt - 1;  
  104.             if (v1 == totalInt) {  
  105.                 v = totalInt - 10;  
  106.             } else if (v == 1 && v1 < totalInt) {  
  107.                 v1 = totalInt > 10 ? 10 : totalInt;  
  108.             }  
  109.   
  110.             //   
  111.             for (int i = v; i <= v1; i++) {  
  112.                 if (cpageInt == i) { // 当前页要加粗显示  
  113.                       
  114.                     str.append("<span class='current'>");  
  115.                     str.append(i);  
  116.                     str.append("</span>");  
  117.                 } else {  
  118.                     str.append("<a href='");  
  119.                     str.append(url);  
  120.                     str.append("?pageNo=");  
  121.                     str.append(i);  
  122.                     str.append(perUrl);  
  123.                     str.append("'>");  
  124.                     str.append(i);  
  125.                     str.append("</a>");  
  126.                 }  
  127.             }  
  128.   
  129.             if (cpageInt < totalInt - 4)  
  130.                 str.append("<span class='gap'>...</span>");  
  131.             // 显示最后一页  
  132.                         if (cpageInt == totalInt) { // 当前页要加粗显示  
  133.                   
  134.                 str.append("<span class='current'>");  
  135.                 str.append(totalInt);  
  136.                 str.append("</span>");  
  137.             } else{  
  138.                 str.append("<a href='");  
  139.                 str.append(url);  
  140.                 str.append("?pageNo=");  
  141.                 str.append(totalInt);  
  142.                 str.append(perUrl);  
  143.                 str.append("'>");  
  144.                 str.append(totalInt);  
  145.                 str.append("</a>");  
  146.             }  
  147.               
  148.             if (cpageInt == totalInt) {  
  149.                 str.append("<span class='disabled'>下一页 >></span>");  
  150.             } else {  
  151.                 str.append("<a href='");  
  152.                 str.append(url);  
  153.                 str.append("?pageNo=");  
  154.                 str.append(cpageInt + 1);  
  155.                 str.append(perUrl);  
  156.                 str.append("'>下一页 >></a>");  
  157.             }  
  158.         }  
  159.   
  160.         str.append("</div>");  
  161.   
  162.         try {  
  163.             writer.write(str.toString());  
  164.         } catch (IOException e) {  
  165.             // TODO Auto-generated catch block  
  166.             e.printStackTrace();  
  167.         }  
  168.   
  169.         return result;  
  170.     }  
  171. }