分页基于Struts

来源:互联网 发布:蔡晨数据 编辑:程序博客网 时间:2024/06/03 16:31

第一:自定义一个分页的类:

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'>首页&nbsp;&nbsp;</a>");
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum-1)+"'>&nbsp;&nbsp;上页&nbsp;&nbsp;</a>");
   } 
   if (pageNum < totalPage)
   {
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum+1)+"'>&nbsp;&nbsp;下页&nbsp;&nbsp;</a>");
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+totalPage+"'>&nbsp;&nbsp;末页</a>");
   }
  }else{
   if (pageNum > 1)
   {  
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum=1'> Home &nbsp;&nbsp;</a>");
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum-1)+"'>&nbsp;&nbsp; Previous &nbsp;&nbsp;</a>");
   } 
   if (pageNum < totalPage)
   {
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+(pageNum+1)+"'>&nbsp;&nbsp; Next &nbsp;&nbsp;</a>");
    out.print("<a href='"+className+".do?"+para+"="+functionName+"&pageNum="+totalPage+"'>&nbsp;&nbsp; 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>  
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <bean:message key="total"/>&nbsp;<%=allCounts %>&nbsp;<bean:message key="tiao"/> &nbsp;<bean:message key="di"/>&nbsp;<%=pageNum %>&nbsp;/&nbsp;<%=allpage %>&nbsp;<bean:message key="page"/>

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Jerry_BJ/archive/2010/06/02/5642447.aspx