使用的时候像这样:
<form action="<%=request.getContextPath();%>/pageAction.do" name ="hello" method="post" >
<test:pager formName="hello"/>
</form>
form的action名字随便写,只要给tag传进formname就可以了。
不过组装显示内容的action里要根据从页面得到的(pageIndex/pageTotal/pageNo)来组装内容列表。
------------------------------------------------------------------------------
等写完整了把完整的放上来,待续......
/*
* Created on 2004-8-6
* author:xmx
*/
package demo.pagetag;
public class PageInfo {
private int _recordTotal;
private int _pageTotal;
private int _currentPage;
private int _pageSize;
/**
* @return
*/
public int getRecordTotal(); {
return _recordTotal;
}
/**
* @param i
*/
public void setRecordTotal(int i); {
_recordTotal = i;
}
/**
* @return
*/
public int getCurrentPage(); {
return _currentPage;
}
/**
* @return
*/
public int getPageTotal(); {
int tmp=_recordTotal/_pageSize;
_pageTotal=(_recordTotal%_pageSize==0);?tmp:tmp+1;
return _pageTotal;
}
/**
* @param i
*/
public void setCurrentPage(int i); {
_currentPage = i;
}
/**
* @param i
*/
public void setPageTotal(int i); {
_pageTotal = i;
}
/**
* @return
*/
public int getPageSize(); {
return _pageSize;
}
/**
* @param i
*/
public void setPageSize(int i); {
_pageSize = i;
}
/**
* 返回当前页起始记录
* @return
*/
public int getStartRecord(); {
if (_pageTotal == 0); {
return 0;
} else {
return (_currentPage - 1); * _pageSize + 1;
}
}
/**
* 返回当前页结尾记录
* @return
*/
public int getEndRecord(); {
if (_pageTotal == 0); {
return 0;
} else if (_pageTotal == _currentPage);{
return _recordTotal;
} else {
return _currentPage * _pageSize ;
}
}
}
/*
* Created on 2004-8-6
* author:xmx
*/
package demo.pagetag;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
public class PageTag extends TagSupport {
protected String name =null;
protected String formName = null;
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
public String getFormName(); {
return formName;
}
public void setFormName(String formName); {
this.formName = formName;
}
/* (non-Javadoc);
* @see javax.servlet.jsp.tagext.Tag#doStartTag();
*/
public int doStartTag(); throws JspException {
String pageName = name;
if (name==null || name.trim();.equals("");); {
pageName = "pageName";
}
PageInfo pInfo = (PageInfo);RequestUtils.lookup(pageContext, pageName, "request");;
String imagePath = ((HttpServletRequest);pageContext.getRequest(););.getContextPath();;
System.out.println("============== Page tag start ===========");;
System.out.println("FormName = "+formName.trim(););;
System.out.println("============== Page tag end ===========");;
System.out.println();;
if (pInfo == null);{
System.out.println("pInfo is null ");;
return SKIP_BODY;
}else{
System.out.println("pInfo.currentPage = "+pInfo.getCurrentPage(););;
}
StringBuffer sb = new StringBuffer();;
createNavigateBar(sb,pInfo,imagePath);;
createJSFunction(sb);;
sb=sb.append("<font size=\"30\" color='red'>hello,小南</font>");;
ResponseUtils.write(pageContext,sb.toString(););;
return (SKIP_BODY);;
}
private void createJSFunction(StringBuffer sb); {
String actionFormName = formName.trim();; //设置Form的名称
sb.append("<script language=\"JavaScript\">\n");;
sb.append("function goPage(pageIndex); {\n");;
sb.append(actionFormName + ".target=\"_self\";\n");;
sb.append(actionFormName + ".pageIndex.value=pageIndex;\n");;
sb.append(actionFormName + ".submit();;\n");;
sb.append("}\n");;
sb.append("function goToPage(); {\n");;
sb.append("var page=" + actionFormName + ".pageNo.value;\n");;
sb.append("if (new Number(page);.toString();==\"NaN\");{\n");;
sb.append("alert(\"请输入 数 字\");;\n");;
sb.append("return;\n");;
sb.append("}else if(page==\"\"||page==null); {\n");;
sb.append("alert(\"请输入 数 字\");;\n");;
sb.append("return;\n");;
sb.append("}\n");;
sb.append("var pageAmount=" + actionFormName + ".pageTotal.value;\n");;
sb.append("if (parseInt(page);>parseInt(pageAmount);); {\n");;
sb.append("page=pageAmount;\n");;
sb.append("}else if (parseInt(page);<1); {\n");;
sb.append("page=1;\n");;
sb.append("}\n");;
sb.append("goPage(page);;\n");;
sb.append("}\n");;
sb.append("</script>\n");;
}
private void createNavigateBar(StringBuffer sb,PageInfo pInfo,String imagePath); {
int currentPage = pInfo.getCurrentPage();;
int pageTotal = pInfo.getPageTotal();;
sb.append("<input type=\"hidden\" name=\"pageIndex\" value=\"" + currentPage + "\">");.append("\n");;
sb.append("<input type=\"hidden\" name=\"pageTotal\" value=\"" + pageTotal + "\">");.append("\n");;
sb.append("<table width=\"100%\" border=\"0\"><tbody><tr>");.append("\n");;
sb.append("<td width=\"20%\" height=\"30\" align=\"center\">第" + currentPage + "页</td>");.append("\n");;
sb.append("<td width=\"15%\" align=\"center\">共" + pageTotal + "页</td>");.append("\n");;
sb.append("<td width=\"15%\" align=\"center\">共" +pInfo.getRecordTotal(); + "条</td>");.append("\n");;
sb.append("<td width=\"50%\" valign=\"center\">");.append("\n");;
sb.append("<a href=\"javascript:goPage(1);;\" style=\"cursor:hand\">首页| </a>");.append("\n");;
int prevPage = 1;
if (currentPage>=2); {
prevPage = currentPage -1;
}
sb.append("<a href=\"javascript:goPage(" + prevPage + ");;\" name=\"page\" style=\"cursor:hand\">前一页| </a>");.append("\n");;
int nextPage = pageTotal;
if (currentPage<pageTotal-1); {
nextPage = currentPage +1;
}
sb.append("<a href=\"javascript:goPage(" + nextPage + ");;\" style=\"cursor:hand\">后一页| </a>");.append("\n");;
int lastPage = pageTotal;
if (pageTotal<2); {
lastPage = 1;
}
sb.append("<a href=\"javascript:goPage(" + lastPage + ");;\" style=\"cursor:hand\">末页| </a>");.append("\n");;
sb.append("转到第<input name=\"pageNo\" type=\"text\" class=\"Input\" size=\"5\">页");.append("\n");;
sb.append("<a href=\"javascript:goToPage();;\" style=\"cursor:hand\">Go</a>");.append("\n");;
sb.append("</td></tr></tbody></table>");.append("\n");;
}
}
在action里使用的时候例子如下:
package demo.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import demo.pagetag.PageInfo;
public class ShowPageInfoAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response);
throws Exception {
System.out.println("ShowPageInfoAction .......");;
//tag 里用到,必须
PageInfo pInfo = new PageInfo();;
pInfo.setPageSize(10);; //recordsNumber/Page
pInfo.setRecordTotal(101);; //到service里拿
setPageInfo(request, pInfo);;
//查询,取到记录
//List list=service.find(pInfo.getStartRecord();,pInfo.getEndRecord(););;
//or
//List list=service.find(pInfo);;
//request.setAttribute("fileList",list);;
return mapping.findForward("success");;
}
private void setPageInfo(HttpServletRequest request, PageInfo pInfo); {
String pageIndex = request.getParameter("pageIndex");;//从页面取到的当前页
if (pageIndex == null || pageIndex.equals("");); {
pInfo.setCurrentPage(1);;
} else {
pInfo.setCurrentPage(Integer.parseInt(pageIndex););;
}
String pageNo = request.getParameter("pageNo");;//跳转的目的页index
if (pageNo != null && !pageNo.equals("");); {
pInfo.setCurrentPage(Integer.parseInt(pageNo););;
}
request.setAttribute("pageName", pInfo);;//tag需要的PageInfo信息
}
}