分享

Struts2中实现自定义分页标签 --功能扩充

 pengx 2008-09-02

Struts2中实现自定义分页标签 --功能扩充

    上一篇结合Struts2实现了分页的自定义标签。标签比较简单,3个参数,单一的显示样式。下面对该标签的功能进行进一步的扩充,主要包括:

1.可以为标签指定样式。通过styleClass属性,可以为标签指定一个样式表。

2.增加了分页样式的选择。通过theme属性指定分页样式。

theme="text"的样式:

theme="number"的样式:

修改方案:

1.在tld文件中增加这两个属性的声明:

Xml代码 复制代码
  1. <attribute>  
  2.                 <name>styleClass</name>  
  3.                 <required>false</required>  
  4.                 <rtexprvalue>true</rtexprvalue>  
  5. </attribute>  
  6. <attribute>  
  7.                 <name>theme</name>  
  8.                 <required>false</required>  
  9.                 <rtexprvalue>true</rtexprvalue>  
  10. </attribute>  

 2.在自定义标签类中分别增加styleClass和theme属性,并提供setter方法:

   PageTag.java

Java代码 复制代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package com.tangs.tag;   
  7.   
  8. import com.opensymphony.xwork2.util.ValueStack;   
  9. import javax.servlet.http.HttpServletRequest;   
  10. import javax.servlet.http.HttpServletResponse;   
  11. import org.apache.struts2.components.Component;   
  12. import org.apache.struts2.views.jsp.ComponentTagSupport;   
  13.   
  14. /**  
  15.  * 分页标签  
  16.  * @author tangs  
  17.  */  
  18. public class PageTag extends ComponentTagSupport {   
  19.     private String cpage;   
  20.     private String total;   
  21.     private String url;   
  22.     private String styleClass;   //新增的样式属性   
  23.     private String theme;   //新增的分页样式属性   
  24.        
  25.     public void setTheme(String theme) {   
  26.         this.theme = theme;   
  27.     }   
  28.        
  29.     public void setStyleClass(String styleClass) {   
  30.         this.styleClass = styleClass;   
  31.     }   
  32.   
  33.     public void setCpage(String cpage) {   
  34.         this.cpage = cpage;   
  35.     }   
  36.   
  37.     public void setTotal(String total) {   
  38.         this.total = total;   
  39.     }   
  40.   
  41.     public void setUrl(String url) {   
  42.         this.url = url;   
  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.setCpage(cpage);   
  55.         pages.setTotal(total);   
  56.         pages.setUrl(url);   
  57.         pages.setStyleClass(styleClass);   
  58.         pages.setTheme(theme);   
  59.   
  60.     }   
  61. }  

   Pages.java

Java代码 复制代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package com.tangs.tag;   
  7.   
  8. import com.opensymphony.xwork2.util.ValueStack;   
  9. import java.io.IOException;   
  10. import java.io.Writer;   
  11. import java.util.logging.Level;   
  12. import java.util.logging.Logger;   
  13. import javax.servlet.http.HttpServletRequest;   
  14. import org.apache.struts2.components.Component;   
  15.   
  16. /**  
  17.  * 分页逻辑Bean  
  18.  * @author tangs  
  19.  */  
  20. public class Pages extends Component {   
  21.     private HttpServletRequest request;   
  22.     private String cpage;   
  23.     private String total;   
  24.     private String url;   
  25.     private String styleClass;   
  26.     private String theme;   
  27.        
  28.     public String getTheme() {   
  29.         return theme;   
  30.     }   
  31.   
  32.     public void setTheme(String theme) {   
  33.         this.theme = theme;   
  34.     }   
  35.        
  36.        
  37.     public String getStyleClass() {   
  38.         return styleClass;   
  39.     }   
  40.   
  41.     public void setStyleClass(String styleClass) {   
  42.         this.styleClass = styleClass;   
  43.     }   
  44.   
  45.        
  46.     public String getCpage() {   
  47.         return cpage;   
  48.     }   
  49.   
  50.     public void setCpage(String cpage) {   
  51.         this.cpage = cpage;   
  52.     }   
  53.   
  54.     public String getTotal() {   
  55.         return total;   
  56.     }   
  57.   
  58.     public void setTotal(String total) {   
  59.         this.total = total;   
  60.     }   
  61.   
  62.     public String getUrl() {   
  63.         return url;   
  64.     }   
  65.   
  66.     public void setUrl(String url) {   
  67.         this.url = url;   
  68.     }   
  69.        
  70.        
  71.     public Pages(ValueStack arg0, HttpServletRequest request) {   
  72.         super(arg0);   
  73.         this.request = request;   
  74.     }   
  75.   
  76.     @Override  
  77.     public boolean start(Writer writer) {   
  78.         boolean result = super.start(writer);   
  79.         try {   
  80.             StringBuilder str = new StringBuilder();   
  81.             boolean isValid = true;   
  82.                
  83.             //从ValueStack中取出数值   
  84.             if (isValid) {   
  85.                 if (total.startsWith("%{") && total.endsWith("}")) {   
  86.                     total = total.substring(2, total.length() -1);   
  87.                     total = (String)this.getStack().findValue(total);   
  88.                     isValid = total == null ? false : true;   
  89.                 } else {   
  90.                     isValid = false;   
  91.                 }   
  92.             }   
  93.             if (isValid) {   
  94.                 if (cpage.startsWith("%{") && cpage.endsWith("}")) {   
  95.                     cpage = cpage.substring(2, cpage.length() - 1);   
  96.                     cpage = (String)this.getStack().findValue(cpage);   
  97.                     isValid = cpage == null ? false : true;   
  98.                 } else {   
  99.                     isValid = false;   
  100.                 }   
  101.             }   
  102.             if (isValid) {   
  103.                 if (url.startsWith("%{") && url.endsWith("}")) {   
  104.                     url = url.substring(2, url.length() - 1);   
  105.                     url = (String)this.getStack().findValue(url);   
  106.                     isValid = url == null ? false : true;   
  107.                 } else {   
  108.                     isValid = false;   
  109.                 }   
  110.             }   
  111.   
  112.             if (isValid) {   
  113.                 Integer cpageInt = Integer.valueOf(cpage);   
  114.                 str.append("<span ");   
  115.                 if (styleClass != null) {   
  116.                     str.append(" class='"+styleClass+"'>");   
  117.                 } else {   
  118.                     str.append(">");   
  119.                 }   
  120.                    
  121.                 //文本样式   
  122.                 if (theme == null || "text".equals(theme)) {  //theme="text"样式   
  123.                     //当前页与总页数相等   
  124.                     if (cpage.equals(total)) {   
  125.                         //如果total = 1,则无需分页,显示“[第1页] [共1页]”   
  126.                         if ("1".equals(total)) {   
  127.                             str.append("[第 " + cpage + " 页]");   
  128.                             str.append(" [共 " + total + " 页]");   
  129.                         } else {   
  130.                             //到达最后一页,显示“[首页] [上一页] [末页]”   
  131.                             str.append("<a href='");   
  132.                             str.append(url);   
  133.                             str.append("?cpage=1&total="+total+"&url="+url);   
  134.                             str.append("'>[首页]</a> <a href='");   
  135.                             str.append(url);   
  136.                             str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url);   
  137.                             str.append("'>[上一页]</a> <a href='");   
  138.                             str.append(url);   
  139.                             str.append("?cpage=" + total + "&total=" + total+"&url="+url);   
  140.                             str.append("'>[末页]</a>");   
  141.                         }   
  142.                     } else {   
  143.                         //当前页与总页数不相同   
  144.                         if ("1".equals(cpage)) {   
  145.                             //第一页,显示“[首页] [下一页] [末页]”   
  146.                             str.append("<a href='");   
  147.                             str.append(url);   
  148.                             str.append("?cpage=1&total="+total+"&url="+url);   
  149.                             str.append("'>[首页]</a> <a href='");   
  150.                             str.append(url);   
  151.                             str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url);   
  152.                             str.append("'>[下一页]</a> <a href='");   
  153.                             str.append(url);   
  154.                             str.append("?cpage=" + total + "&total=" + total+"&url="+url);   
  155.                             str.append("'>[末页]</a>");   
  156.                         } else {   
  157.                             //不是第一页,显示“[首页] [上一页] [下一页] [末页]”   
  158.                             str.append("<a href='");   
  159.                             str.append(url);   
  160.                             str.append("?cpage=1&total="+total+"&url="+url);   
  161.                             str.append("'>[首页]</a> <a href='");   
  162.                             str.append(url);   
  163.                             str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url);   
  164.                             str.append("'>[上一页]</a> <a href='");   
  165.                             str.append(url);   
  166.                             str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url);   
  167.                             str.append("'>[下一页]</a> <a href='");   
  168.                             str.append(url);   
  169.                             str.append("?cpage=" + total + "&total=" + total+"&url="+url);   
  170.                             str.append("'>[末页]</a>");   
  171.                         }   
  172.                     }   
  173.                 } else if ("number".equals(theme)) {  //theme="number"的数字样式 [1 2 3 4 5 6 7 8 9 10 > >>]   
  174.                     Integer totalInt = Integer.valueOf(total);   
  175.                       
  176.                     //如果只有一页,则无需分页   
  177.                     str.append("[ ");   
  178.                     if (totalInt == 1) {   
  179.                         str.append("<strong>1</strong> ");   
  180.                     } else {   
  181.                         //计算一共分几组   
  182.                         int group = (totalInt - 1) / 10 + 1;   
  183.                         //当前第几组   
  184.                         int cgroup = (cpageInt - 1) / 10 + 1;   
  185.                            
  186.                         if (cgroup > 1) {   
  187.                             //当前不是第一组,要显示“<< <”   
  188.                             //<<:返回前一组第一页   
  189.                             //<:返回前一页   
  190.                             str.append("<a href='");   
  191.                             str.append(url);   
  192.                             str.append("?cpage=" + ((cgroup - 2) * 10 + 1 ) + "&total=" + total+"&url="+url);   
  193.                             str.append("'>«</a> " );   
  194.                             str.append("<a href='");   
  195.                             str.append(url);   
  196.                             str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url);   
  197.                             str.append("'>‹</a> " );   
  198.                         }   
  199.                         //10个为一组显示   
  200.                         for (int i = (cgroup - 1) * 10 + 1; i <= totalInt && i <= cgroup * 10; i++) {   
  201.                             if (cpageInt == i) { //当前页要加粗显示   
  202.                                 str.append("<strong>");     
  203.                             }   
  204.                             str.append("<a href='");   
  205.                             str.append(url);   
  206.                             str.append("?cpage=" + i + "&total=" + total+"&url="+url);   
  207.                             str.append("'>" + i + "</a> ");   
  208.                             if (cpageInt == i) {   
  209.                                 str.append("</strong>");   
  210.                             }   
  211.                         }   
  212.                         //如果多于1组并且不是最后一组,显示“> >>”   
  213.                         if (group > 1&& cgroup != group) {   
  214.                             //>>:返回下一组最后一页   
  215.                             //>:返回下一页   
  216.                             str.append("<a href='");   
  217.                             str.append(url);   
  218.                             str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url);   
  219.                             str.append("'>›</a> " );   
  220.                             str.append("<a href='");   
  221.                             str.append(url);   
  222.                             str.append("?cpage=" + ((cgroup * 10 + 10) > totalInt ? totalInt : (cgroup * 10 + 10)) + "&total=" + total+"&url="+url);   
  223.                             str.append("'>»</a> " );   
  224.                         }   
  225.                     }   
  226.                     str.append("]");   
  227.                 }   
  228.                 str.append("</span>");   
  229.             }   
  230.             
  231.             writer.write(str.toString());   
  232.                
  233.         } catch (IOException ex) {   
  234.             Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);   
  235.         }   
  236.         return result;   
  237.     }   
  238. }  

 3.在页面中使用标签

Html代码 复制代码
  1. <tangs:pages cpage="%{cpage}" total="%{total}" url="%{url}" styleClass="page" theme="number"/>  

 指定了styleClass和theme属性,得到的分页样式:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多