Struts2/webwork的分页实现(tag)

gretaguo 2009-03-21

在每次使用struts2/webwork中使用分页的时候都感觉特别麻烦,主要是标签写太多又复杂,于是就想能不能通过自定义标签进行分装分页呢?如,只需要在JSP界面上写<分页></分页>这样就ok了

 具体实现如下:

  1 :在需要分页的jsp界面 写下标签

         

 <app:pagerequest action="buycompany"  css_class_name="page"

    next="next" nextname="<fmt:message key="nextPage"/>"

    last="last" lastname="<fmt:message key="lastPage"/>" " currentPage="currentPage" 

    currentPagename="第"    currentPageValue="${current}"

    allPagename="总共"    allSize="${allPage}" allSizename="pageSize"     jump="第" page="页"/>

       最终显示如图Struts2/webwork的分页实现(tag)

   1),<app: 是自己自定标签  就是下面加载标签的 prefix="app"

   2), pagerequest: 分页自定义功能对应tag name

   3),action="buycompany" :分页提交查看的对应分页action

  2 :在jsp中加载标签

    <%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>

    这个一般我是写在tagsupport.jsp里面

    具体如下

 <%@ page language="java" errorPage="/error.jsp"

                    pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

<%@tagliburi="http://www.opensymphony.com/sitemesh/page"prefix="page"%>

<%@tagliburi="http://java.sun.com/jstl/fmt"prefix="fmt"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>

所以在所有的jsp中include该tagsupport.jsp就可以了

 3:WEB-INF/建立app.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>1.0</tlib-version>

<jsp-version>1.2</jsp-version>

   <short-name>app</short-name>

 <tag>

      <name>pagerequest</name>      <tag-class>com.。。。。。.taglib.PageRequestTag</tag-class>

      <attribute>

<name>action</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>next</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>last</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>currentPageName</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>currentPage</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>currentPageValue</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>allSize</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>allSizeName</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>lastName</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>nextName</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>css_class_name</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>allPageName</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>jump</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>page</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

   </tag>

</taglib>

1)com.。。。。。taglib.PageRequestTag 就是我们自己写的自定义标签 其内容如下面第四部所讲

4:在自己的一个包中写入PageRequestTag.java

  具体内容如下:

     package com.sky.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.TagSupport;

 /**

   *分页自定义标签

*author:haiwoo

  */

public class PageRequestTag extends TagSupport {

 /**

*

*/

privatestaticfinallongserialVersionUID=-3210838227548318578L;

//查看看分页的action

    private String action;

  //下一页 主要是写在li的id值    private String next;

 //上一页 主要是写在li的id值

privateStringlast;

    private String currentPage;

    //当前页    private String currentPageName;

   //但前页的值    private String currentPageValue;

  //上一页的名称:中文 =上一页 英文=lastPage    private String lastName;

 //上一页的名称:中文 =下一页 英文=nextPage

privateStringnextName;

privateStringallSize;

    private String allSizeName;

  //分页的样式

privateStringcss_class_name;

privateStringjump;

privateStringpage;

privateStringallPageName;

   

 

publicvoidsetAction(Stringaction){

this.action=action;

}

publicvoidsetAllSize(StringallSize){

this.allSize=allSize;

 }

publicvoidsetCss_class_name(Stringcss_class_name){

this.css_class_name=css_class_name;

 }

 public void setLast(String last) {

this.last=last;

 }

 public void setNext(String next) {

this.next=next;

 } 

 

publicvoidsetJump(Stringjump){

this.jump=jump;

 }

 public void setPage(String page) {

this.page=page;

 }

publicvoidsetAllSizeName(StringallSizeName){

this.allSizeName=allSizeName;

 }

 public void setLastName(String lastName) {

this.lastName=lastName;

 }

 public void setNextName(String nextName) {

this.nextName=nextName;

}

publicvoidsetCurrentPage(StringcurrentPage){

this.currentPage=currentPage;

}

 

 public void setAllPageName(String allPageName) {

this.allPageName=allPageName;

 }

 public void setCurrentPageName(String currentPageName) {

this.currentPageName=currentPageName;

 }

 public void setCurrentPageValue(String currentPageValue) {

this.currentPageValue=currentPageValue;

 }

 @Override

publicintdoStartTag()throwsJspException{

StringBuffersb=newStringBuffer();

intcurrent=Integer.valueOf(currentPageValue);

intlastPage=0;

intnextPage=0;

intallPage=Integer.valueOf(allSize);

if(allPage<1)allPage=1;

if(current>1)lastPage=current-1;

elselastPage=1;

if(current<allPage)nextPage=current+1;

elsenextPage=current;

sb.append("<inputtype=\"hidden\"name=\""+next+"\"id=\""+next+"\"value=\""+nextPage+"\">\n");

sb.append("<inputtype=\"hidden\"name=\""+last+"\"id=\""+last+"\"value=\""+lastPage+"\">\n");

sb.append("<ulclass=\""+css_class_name+"\">");

//上一页

sb.append("<li><ahref=\"#\""+

"onclick=\"javascript:document.getElementById(\'"+currentPage+"\').value=document.getElementById(\'"+last+"\').value,"+

"document.getElementById(\'"+action+"\').submit()\">"+

lastName+"</a></li>");

//当前第几页

sb.append("<li>"+currentPageName+current+page+"</li>");

//下一页

sb.append("<li><ahref=\"#\""+

"onclick=\"javascript:document.getElementById(\'"+currentPage+"\').value=document.getElementById(\'"+next+"\').value,"+

"document.getElementById(\'"+action+"\').submit()\">"+

nextName+"</a></li>");

//调转到第几页

sb.append("<li>"+jump+"<selectname=\""+

allSizeName+"\"id=\""+allSizeName+

"\""+

"onchange=\"javascript:document.getElementById(\'"+currentPage+"\').value=this.value,"+

"document.getElementById(\'"+action+"\').submit()\""+

"class=\"select\">\n");

if("0".equals(allSize)||allSize==null||"".equals(allSize)){

sb.append("<optionvalue=\"0\"");

sb.append(">0</option>\n");

}else{

for(inti=1;i<=allPage;i++){

sb.append("<optionvalue=\""+i+"\"");

sb.append(">"+i+"</option>\n");

}

         }

         sb.append("</select></li>"+page);

//总共多少页

sb.append("<li>"+allPageName+allPage+page+"</li>");

sb.append("</ul>");

try{

pageContext.getOut().write(sb.toString());

}catch(IOExceptionio){

thrownewJspException(io);

}

returnsuper.doStartTag();

 }

 @Override

publicvoidrelease(){

super.release();

 }

}

 5:在action中查询分页

  //查询总共多少页

   int size=manager.getObjectCount( );   allPage=(size+Constants.Page_MaxLength_Size)/Constants.Page_MaxLength_Size; 

 // 查询第几页

   int start=(currentPage-1)*Constants.Page_MaxLength_Size-1;    if(start<0)start=0;  

//这是查询结果   list=manager.getlist(start, Constants.Page_MaxLength_Size);

 1) allPage 在action 中定义  我是在baseAction中定义的

          /**

*当前页默认为第一页开始

*/

protectedIntegercurrentPage=Integer.valueOf(1);

/**

*总共多少页默认只有一页

*/

     protected Integer allPage=Integer.valueOf(1);

 

    并有相应的setter与getter

 2) Constants.Page_MaxLength_Size 一页显示多少条记录 这是静态final变量

 

      

总结:

 好的一方面

根据上面的结果 只要我想要的分页的地方只要写下如下俩个地方

 jsp:

      <app:pagerequest action="buycompany"  css_class_name="page"

    next="next" nextname="<fmt:message key="nextPage"/>"

    last="last" lastname="<fmt:message key="lastPage"/>" " currentPage="currentPage" 

    currentPagename="第"    currentPageValue="${current}"

    allPagename="总共"    allSize="${allPage}" allSizename="pageSize"     jump="第" page="页"/>

action:

  //查询总共多少页

   int size=manager.getObjectCount( );   allPage=(size+Constants.Page_MaxLength_Size)/Constants.Page_MaxLength_Size; 

 // 查询第几页

   int start=(currentPage-1)*Constants.Page_MaxLength_Size-1;    if(start<0)start=0;  

//这是查询结果   list=manager.getlist(start, Constants.Page_MaxLength_Size);

  不好的地方

 1:Constants.Page_MaxLength_Size 一页显示多少条记录 修改不方便 :可以改进为从界面传递下去

 2:没有跟ajax进行结合  每次查看分页都刷新了整个界面: 改进成ajax方式提交

相关推荐

zhangchaoming / 0评论 2020-01-11
林大夏 / 0评论 2019-12-21