码迷,mamicode.com
首页 > 其他好文 > 详细

struts2的分页标签

时间:2014-11-20 11:52:22      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

1、准备tld文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description><![CDATA["To make it easier to access dynamic data;
                        the Apache Struts framework includes a library of custom tags.
                        The tags interact with the frameworks validation and internationalization features;
                        to ensure that input is correct and output is localized.
                        The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
  <display-name>LetGo</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>voice</short-name>
  <uri>/page-tags</uri>
  <tag>
    <description><![CDATA[Render a paging link tag]]></description>
    <name>paging</name>
    <tag-class>com.page.PagingAction</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <description><![CDATA[current page index]]></description>
      <name>currPage</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[The total page]]></description>
      <name>totalPage</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[The page max]]></description>
      <name>pageMax</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[total link number for show]]></description>
      <name>count</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>

2、写相应的处理类 继承的是 jsp-api.jar 中 SimpleTagSupport,在该类中同时也可以设置各个按钮的js事件....

bubuko.com,布布扣
  1 package com.page;
  2 
  3 import java.io.IOException;
  4 
  5 import javax.servlet.jsp.JspException;
  6 import javax.servlet.jsp.JspWriter;
  7 import javax.servlet.jsp.tagext.SimpleTagSupport;
  8 
  9 import com.opensymphony.xwork2.ActionContext;
 10 
 11 public class PagingAction extends SimpleTagSupport{
 12 
 13     private String currPage;
 14     private String totalPage;
 15     private String pageMax;
 16     private String count;
 17 
 18     /**
 19      * 返回  
 20      * @return 
 21      */
 22     public String getCurrPage() {
 23         return currPage;
 24     }
 25 
 26     /**
 27      * 设置  
 28      * @param 
 29      */
 30     public void setCurrPage(String currPage) {
 31         this.currPage = currPage;
 32     }
 33 
 34     /**
 35      * 返回  
 36      * @return 
 37      */
 38     public String getTotalPage() {
 39         return totalPage;
 40     }
 41 
 42     /**
 43      * 设置  
 44      * @param 
 45      */
 46     public void setTotalPage(String totalPage) {
 47         this.totalPage = totalPage;
 48     }
 49 
 50     /**
 51      * 返回  
 52      * @return 
 53      */
 54     public String getPageMax() {
 55         return pageMax;
 56     }
 57 
 58     /**
 59      * 设置  
 60      * @param 
 61      */
 62     public void setPageMax(String pageMax) {
 63         this.pageMax = pageMax;
 64     }
 65 
 66     /**
 67      * 返回  
 68      * @return 
 69      */
 70     public String getCount() {
 71         return count;
 72     }
 73 
 74     /**
 75      * 设置  
 76      * @param 
 77      */
 78     public void setCount(String count) {
 79         this.count = count;
 80     }
 81 
 82     @Override
 83     public void doTag() throws JspException, IOException {
 84         JspWriter write = getJspContext().getOut();
 85         StringBuffer sb = new StringBuffer();
 86         sb.append("<div>")
 87         .append("<button name=\"first\"><<</button>")
 88         .append("<button name=\"pre\"><</button>")
 89         .append("<input type=\"text\" name=\"inputName\" style=\"width: 45px;\"/>")
 90         .append("<label>"+getValueFromStack(this.getCurrPage())+"/共"+getValueFromStack(this.getTotalPage())+"</label>")
 91         .append("<button name=\"next\">></button>")
 92         .append("<button name=\"last\">>></button>")
 93         .append("<div>");
 94         write.write(sb.toString());
 95     }
 96     
 97     /**
 98      * 在值栈中取出值
 99      * @param str
100      * @return
101      */
102     private String getValueFromStack(String str){
103         //取出时不要加%{},直接是存放在值栈中的变量名,此处获取到str是从页面标签传过的值,jia
104         //加%{}用以区分这个是从值栈中取出,对于不加%{}的属性表示表单提交的变量名称 相当于<input 中的 name属性
105         //action中的get方法名与之相同,则可获取由标签传递的值
106         if(str.startsWith("%{")){
107             str = str.substring(2,str.length()-1);
108         }
109         return (String) ActionContext.getContext().getValueStack().findValue(str);
110     }
111 
112 }
View Code

3、在jsp页面引用改标签

OK!!!

bubuko.com,布布扣

struts2的分页标签

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/Wen-yu-jing/p/4110107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!