SSH自定义标签分页

[代码] 自定义标签库中的用于输出表格分页按钮的组件
view sourceprint?
01 public class PageTagComponent extends Component { 

02     /** 

03      * 分页工具 

04      */

05     private PageUtil pageUtil; 

06     /** 

07      * 数据读取URL 

08      */

09     private String action = "#"; 

10   

11     private String firstPageHTML = ""; 

12     private String prePageHTML = ""; 

13     private String nextPageHTML = ""; 

14     private String lastPageHTML = ""; 

15     private String currentPageHTML = ""; 

16     private String totalPageHTML = ""; 

17   

18     public PageTagComponent(ValueStack stack) { 

19         super(stack); 

20     } 

21   

22     /** 

23      * <div> 

24      * <a>首页</a><a>上一页</a><a>下一页</a><a>尾页</a><span>当前页:13</span><span>总页数:54 

25      * </span> </div> 

26      */

27     @Override

28     public boolean start(Writer writer) { 

29         generateHTML(); 

30         StringBuilder sb = new StringBuilder(); 

31         sb.append(firstPageHTML); 

32         sb.append(prePageHTML); 

33         sb.append(nextPageHTML); 

34         sb.append(lastPageHTML); 

35         sb.append(currentPageHTML); 

36         sb.append(totalPageHTML); 

37   

38         try { 

39             writer.write(sb.toString()); 

40         } catch (IOException e) { 

41             e.printStackTrace(); 

42         } 

43         return super.start(writer); 

44     } 

45   

46     private void generateHTML() { 

47         if (pageUtil.getCurrentPageCount() - 1 <= 1) { 

48             prePageHTML = "<a href='" + action + "?pageUtil.currentPageCount=1'>上一页</a>"; 

49         } else { 

50             prePageHTML = "<a href='" + action + "?pageUtil.currentPageCount=" + (pageUtil.getCurrentPageCount() - 1) + "'>上一页</a>"; 

51         } 

52   

53         if (pageUtil.getCurrentPageCount() + 1 >= pageUtil.getPageCount()) { 

54             nextPageHTML = "<a href='" + action + "?pageUtil.currentPageCount=" + pageUtil.getPageCount() + "'>下一页</a>"; 

55         } else { 

56             nextPageHTML = "<a href='" + action + "?pageUtil.currentPageCount=" + (pageUtil.getCurrentPageCount() + 1) + "'>下一页</a>"; 

57         } 

58   

59         firstPageHTML = "<a href='" + action + "?pageUtil.currentPageCount=1'>首页</a>"; 

60         lastPageHTML = "<a href='" + action + "?pageUtil.currentPageCount=" + pageUtil.getPageCount() + "'>尾页</a>"; 

61         currentPageHTML = "<span>当前页:" + pageUtil.getCurrentPageCount() + "</span>"; 

62         totalPageHTML = "<span>总页数:" + pageUtil.getPageCount() + "</span>"; 

63     } 

64 }
[代码] 自定义的分页标签
view sourceprint?
01 public class PageTag extends ComponentTagSupport { 

02     /** 

03      * 分页工具 

04      */

05     private PageUtil pageUtil; 

06     /** 

07      * 数据读取URL 

08      */

09     private String action = "#"; 

10   

11     @Override

12     public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) { 

13         return new PageTagComponent(valueStack); 

14     } 

15   

16     @Override

17     protected void populateParams() { 

18         super.populateParams(); 

19         PageTagComponent pageTageComponent = (PageTagComponent) component; 

20         pageTageComponent.setPageUtil(pageUtil); 

21         pageTageComponent.setAction(action); 

22     } 

23 }
[代码] Action中处理用户请求的方法
view sourceprint?
1 public String Search() { 

2     pageUtil = ipAddressService.getAllIpAddress(pageUtil,ip); 

3     return "Search"+ SUCCESS; 

4 }
[代码] Service中初始化分页组件
view sourceprint?
1 public PageUtil getAllIpAddress(PageUtil pageUtil, String ip) { 

2     pageUtil.setTotalCount(getAllIpAddress().size()); 

3     return ipAddressHibernateDao.queryAllEntity("from IpAddress where ip = " + ip, pageUtil); 

4 }
[代码] Dao中的分页查询
view sourceprint?
01 public PageUtil queryAllEntity(final String hql, PageUtil pageUtil) { 

02     if (pageUtil == null) { 

03         pageUtil = new PageUtil(); 

04     } 

05     final int startRows = pageUtil.getPageSize() * (pageUtil.getCurrentPageCount() - 1); 

06     final int rowSize = pageUtil.getPageSize(); 

07     List result = getHibernateTemplate().executeFind(new HibernateCallback() { 

08         @Override

09         public Object doInHibernate(Session session) throws HibernateException, SQLException { 

10             Query query = session.createQuery(hql); 

11             query.setFirstResult(startRows); 

12             query.setMaxResults(rowSize); 

13             return query.list(); 

14         } 

15     }); 

16     pageUtil.setDatas(result); 

17     return pageUtil; 

18 }
[代码] JSP中tag的使用以及表格内容的显示
view sourceprint?
01 <%@taglib prefix="wu" uri="/web-utils" %> 

02         <table class="table" width="850px"> 

03             <thead> 

04                 <tr> 

05                     <th width="50px"><s:text name="title.index"/></th> 

06                     <th width="100px"><s:text name="title.ipAddress.ip"/></th> 

07                     <th width="100px"><s:text name="title.ipAddress.mac"/></th> 

08                     <th width="100px"><s:text name="title.ipAddress.switchIP"/></th> 

09                     <th width="100px"><s:text name="title.ipAddress.port"/></th> 

10                     <th width="100px"><s:text name="title.ipAddress.addressRemark"/></th> 

11                     <th width="100px"><s:text name="title.ipAddress.addressState"/></th> 

12                     <th width="100px"><s:text name="title.ipAddress.addressType"/></th> 

13                     <th width="100px"><s:text name="title.operation"/></th> 

14                 </tr> 

15             </thead> 

16             <tbody> 

17                 <s:if test="pageUtil.datas.size > 0"> 

18                     <s:iterator value="pageUtil.datas" status="ipAddress"> 

19                         <tr> 

20                             <td width="50px"><s:property value="#ipAddress.index + 1"/></td> 

21                             <td width="100px"><a href="<s:url action='ipAddressDetailAction' namespace='/admin'/>?id=${id}"><s:property value="ip"/></a></td> 

22                             <td width="100px"><s:property value="mac"/></td> 

23                             <td width="100px"><s:property value="switchIp"/></td> 

24                             <td width="100px"><s:property value="port"/></td> 

25                             <td width="100px"><a href="<s:url action='ipAddressRemarkDetailAction' namespace='/admin'/>?id=${ipAddressRemark.id}"><s:property value="ipAddressRemark.remarkName"/></a></td> 

26                             <td width="100px"><a href="<s:url action='ipAddressStateDetailAction' namespace='/admin'/>?id=${ipAddressState.id}"><s:property value="ipAddressState.stateName"/></a></td> 

27                             <td width="100px"><a href="<s:url action='ipAddressTypeDetailAction' namespace='/admin'/>?id=${ipAddressType.id}"><s:property value="ipAddressType.typeName"/></a></td> 

28                             <td width="100px"> 

29                                 <a href="<s:url action='ipAddressEditPageAction' namespace='/admin'/>?id=${id}"><s:text name="button.edit"></s:text></a> 

30                                 <a href="<s:url action='ipAddressDeleteAction' namespace='/admin'/>?id=${id}"><s:text name="button.delete"></s:text></a> 

31                             </td> 

32                         </tr> 

33                     </s:iterator> 

34                 </s:if> 

35                 <s:else> 

36                     <tr><td colspan="9" class="tdNoData"><s:text name="label.noData"/></td></tr> 

37                 </s:else> 

38             </tbody> 

39         </table> 

40         <s:if test="pageUtil.pageCount > 1"> 

41             <div class="pageTag" style="width: 850px;"> 

42                 <s:url action='ipAddressListAction' namespace='/admin' var="address"/> 

43                 <wu:page action="${address}" pageUtil="${pageUtil}"/> 

44             </div> 

45         </s:if>
[代码] 自定义标签配置文件
view sourceprint?01 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 

02 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

03     version="2.0"

04     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> 

05     <display-name>"Web Utils"</display-name> 

06     <tlib-version>1.0</tlib-version> 

07     <short-name>wu</short-name> 

08     <uri>/web-utils</uri> 

09     <tag> 

10         <description><![CDATA[Print table pages]]></description> 

11         <name>page</name> 

12         <tag-class>com.kmairport.webapp.tag.PageTag</tag-class> 

13         <body-content>empty</body-content> 

14         <attribute> 

15             <description><![CDATA[request address]]></description> 

16             <name>action</name> 

17             <required>true</required> 

18             <rtexprvalue>true</rtexprvalue> 

19         </attribute> 

20         <attribute> 

21             <description><![CDATA[page util]]></description> 

22             <name>pageUtil</name> 

23             <required>true</required> 

24             <rtexprvalue>true</rtexprvalue> 

25         </attribute> 

26     </tag> 

27 </taglib> 

28 备注:该文件保存为web-utils.tld,放在WEB-INF/目录下

你可能感兴趣的:(Web)