代码都是直接从 项目中 摘出来的...
第一:自定义一个分页的类:
package com.ygkq.usermanager.view.taglib;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class DoFenYeTaglib extends SimpleTagSupport {
private int pageNum;
private int totalPage;
private String className;
private String para;
private String functionName;
String languageDefault = Locale.getDefault().getLanguage();
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public void doTag() throws JspException, IOException {
JspWriter out = this.getJspContext().getOut();
if (languageDefault == "zh" || languageDefault.equals("zh")){
if (pageNum > 1)
{
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum=1'>首页 </a>");
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum-1)+"'> 上页 </a>");
}
if (pageNum < totalPage)
{
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum+1)+"'> 下页 </a>");
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+totalPage+"'> 末页</a>");
}
}else{
if (pageNum > 1)
{
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum=1'> Home </a>");
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum-1)+"'> Previous </a>");
}
if (pageNum < totalPage)
{
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum+1)+"'> Next </a>");
out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+totalPage+"'> End </a>");
}
}
super.doTag();
}
}
Action 里面的某个方法;
//用户查询充值记录
@SuppressWarnings("unchecked")
public ActionForward getAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
int count = 2;
String pageNumStr = request.getParameter("pageNum");
if (pageNumStr == null)
pageNumStr = "1";
int pageNum = Integer.parseInt(pageNumStr);
int start = count * (pageNum - 1);
ServletContext application = request.getSession().getServletContext();
WebApplicationContext webContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(application);
UserService mems = (UserService) webContext
.getBean("userService");
UserBasic luser = (UserBasic)session.getAttribute("user");
String cn = luser.getCn();
List allLogs = null;
int allCounts = 0;
int allpage = 0;
try {
allLogs = mems.getAllByCn(cn,count, start);
allCounts = mems.getAllLogNumByCn(cn);
allpage = (allCounts + count - 1) / count;
request.setAttribute("allLogs", allLogs);
request.setAttribute("pageNum", pageNum);
request.setAttribute("allpage", allpage);
request.setAttribute("allCounts", allCounts);
return mapping.findForward("query_ok");
} catch (RuntimeException e) {
logger.error(e);
return mapping.findForward("error");
}
}
在 WEB-INF下面新建一个mytag.tld文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>my</shortname>
<tag>
<name>fenye</name>
<tagclass>com.ygkq.usermanager.view.taglib.DoFenYeTaglib</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>pageNum</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>totalPage</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>className</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>para</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>functionName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
最后就是JSP页面了;
在JSP前面声明一下这个标签的位置
<%@ taglib uri="/WEB-INF/mytag.tld" prefix="f"%>
这些事从struts里面传出来的
int pageNum = (Integer) request.getAttribute("pageNum");
int allpage = (Integer) request.getAttribute("allpage");
int allCounts = (Integer) request.getAttribute("allCounts");
List allLogs = (List) request.getAttribute("allLogs");
接着就是分页的标签引用了...
<center>
<f:fenye className="doUser" para="opers" functionName="getAll" pageNum="<%=pageNum%>" totalPage="<%=allpage %>" /></center>
<bean:message key="total"/> <%=allCounts %> <bean:message key="tiao"/> <bean:message key="di"/> <%=pageNum %> / <%=allpage %> <bean:message key="page"/>
这样就差不多了,不懂的可以E-mian:jerry8059@163.com or QQ:251637313