首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2 扩充标签之分页标签 PageTag

2012-08-24 
Struts2 扩展标签之分页标签 PageTag扩展一个分页标签(1)扩展:org.apache.struts2.views.jsp.ComponentTag

Struts2 扩展标签之分页标签 PageTag
扩展一个分页标签
(1)
扩展:org.apache.struts2.views.jsp.ComponentTagSupport
一个扩展类 ExtTag

public class ExtTag extends ComponentTagSupport{/** * 样式 */protected String theme;/** *  id */protected String id;public String getTheme() {return theme;}public void setTheme(String theme) {this.theme = theme;}public String getId() {return id;}public void setId(String id) {this.id = id;} @Overridepublic Component getBean(ValueStack arg0, HttpServletRequest arg1,HttpServletResponse arg2) {return null;}}

分页标签
public class PageTag extends ExtTag{/** * 每页大小  */private String pagesize;/** * 总记录数 */private String total;/** * 当前页 */private String currentpage;/** * url */private String url;/** *url类别  */private String urlType;/** * 单位名称 */private String unit; @Overridepublic Component getBean(ValueStack arg0, HttpServletRequest arg1,HttpServletResponse arg2) {super.populateParams();            return  new Page(arg0);}/** * 传递参数 */    protected void populateParams() {          super.populateParams();          Page page = (Page)component;          page.setCurrentpage(currentpage);        page.setId(id);        page.setPagesize(pagesize);        page.setTheme(theme);        page.setTotal(total);        page.setUrl(url);        page.setUrlType(urlType);        page.setUnit(unit);    } public String getPagesize() {return pagesize;}public void setPagesize(String pagesize) {this.pagesize = pagesize;}public String getTotal() {return total;}public void setTotal(String total) {this.total = total;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUrlType() {return urlType;}public void setUrlType(String urlType) {this.urlType = urlType;}public String getCurrentpage() {return currentpage;}public void setCurrentpage(String currentpage) {this.currentpage = currentpage;}public String getUnit() {return unit;}public void setUnit(String unit) {this.unit = unit;}}


(2)
扩展控件:org.apache.struts2.components.Component
public class ExtComponent extends Component{public ExtComponent(ValueStack arg0) {super(arg0);}/** * 样式 */protected String theme;/** *  id */protected String id;public String getTheme() {return theme;}public void setTheme(String theme) {this.theme = theme;}public String getId() {return id;}public void setId(String id) {this.id = id;}}

分页控件
public class Page extends ExtComponent{/** * 每页大小  */private String pagesize;/** * 总记录数 */private String total;/** * 当前页 */private String currentpage;/** * url */private String url;/** *url类别  */private String urlType;/** * @param stack *//** * 单位名称 */private String unit;public Page(ValueStack arg0) {super(arg0);}/** *   */public boolean start(Writer writer) {try {StringBuffer html = new StringBuffer();int _pagesize = 15;if (!CheckUtil.isNullOrEmpty(pagesize)) {   _pagesize = StringUtil.parsetoInt(pagesize, 15);}int _total = StringUtil.parsetoInt(total, 0);int _currentpage = StringUtil.parsetoInt(currentpage, 1);if (_currentpage < 1) {_currentpage = 1;}int _totalPage = (int)Math.ceil(_total * 1.0f/_pagesize);    if (CheckUtil.isNullOrEmpty(unit)) {    unit = "条";    }    html.append("<div class="fer_pager");if (!CheckUtil.isNullOrEmpty(theme)) {html.append(" " + theme);}html.append("" ");    if (!CheckUtil.isNullOrEmpty(id)) {    html.append(" id="" + id + """);    }    html.append(">");    html.append("<ul class="kptturnpage pagerpanel">");html.append("<li class="ntotal">当前第<i class="currentpage">" + _currentpage + "</i>页/共<i>" + _totalPage + "</i>页  每页<i>" + _pagesize + "</i>" + unit + "/共<i class="totalNum">" + _total +"</i>" + unit + "</li>");if (_totalPage > 0) {if (_currentpage <= 1){html.append("<li class="nfirst"><a title="第一页"></a></li>");html.append("<li class="nprevior"><a title="上一页"></a></li>");    }else {html.append("<li class="first"><a " + go("第一页", 1, url)+ "></a></li>");int _previosPage = _currentpage - 1 > 0? (_currentpage - 1):1;html.append("<li class="previor"><a " + go("上一页", _previosPage, url)+ "></a></li>");}if (_currentpage >= _totalPage) {html.append("<li class="nnext"><a title="下一页" href="javascript:void(0);"></a></li>");html.append("<li class="nlast"><a title="末页" href="javascript:void(0);"></a></li>");}else {int _nextPage = _currentpage + 1 <= _totalPage? (_currentpage + 1): _totalPage;html.append("<li class="next"><a " + go("下一页", _nextPage, url)+ "></a></li>");html.append("<li class="last"><a " + go("末页", _totalPage, url)+ "></a></li>");}}html.append("</ul></div>");writer.write(html.toString());   } catch (Exception e) {e.printStackTrace();}return true;}    /**     * 跳转到哪一页     */    private String go(String title, int page,String url){    StringBuffer sb = new StringBuffer();    sb.append(" title="" + title + "" ");    if (CheckUtil.isNullOrEmpty(url)) {    sb.append(" href="javascript:void(0);" ");    sb.append(" onclick="PAS.page.go(" + page+")" ");    }    else {    sb.append(" href="");    sb.append(url);    if (url.indexOf('?') > -1) {    sb.append("&");    }    else {    sb.append("?");     }    sb.append("page=" + page);     sb.append(""");    }    return sb.toString();    }public String getPagesize() {return pagesize;}public void setPagesize(String pagesize) {this.pagesize = pagesize;}public String getTotal() {return total;}public void setTotal(String total) {this.total = total;}public String getCurrentpage() {return currentpage;}public void setCurrentpage(String currentpage) {this.currentpage = currentpage;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUrlType() {return urlType;}public void setUrlType(String urlType) {this.urlType = urlType;}public String getUnit() {return unit;}public void setUnit(String unit) {this.unit = unit;}}


(3)
标签tld描述
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib><tlib-version>2.2.3</tlib-version><jsp-version>1.2</jsp-version><short-name>ext</short-name><uri>/ext-tages</uri><display-name>"Ext Tags"</display-name>   <!-- 分页标签 -->   <tag>       <name>page</name>       <tag-class>***.PageTag</tag-class>       <body-content>empty</body-content>       <attribute>           <name>pagesize</name>           <required>false</required>           <rtexprvalue>true</rtexprvalue>       </attribute>       <attribute>           <name>total</name>           <required>true</required>           <rtexprvalue>true</rtexprvalue>       </attribute>       <attribute>           <name>currentpage</name>           <required>true</required>           <rtexprvalue>true</rtexprvalue>       </attribute>       <attribute>           <name>url</name>           <required>false</required>           <rtexprvalue>true</rtexprvalue>       </attribute>       <attribute>           <name>urlType</name>           <required>false</required>           <rtexprvalue>true</rtexprvalue>       </attribute>       <attribute>           <name>theme</name>           <required>false</required>           <rtexprvalue>true</rtexprvalue>       </attribute>         <attribute>           <name>id</name>           <required>false</required>           <rtexprvalue>true</rtexprvalue>       </attribute>                         </tag></taglib> 


(4)
页面调用
<%@taglib uri="/WEB-INF/tags/ext-tags.tld" prefix="ext" %><ext:page total="${total}" currentpage="${page}" pagesize="${pagesize}"/>

热点排行