struts分页技术源码(一)

来源:互联网 发布:java如何避免内存泄露 编辑:程序博客网 时间:2024/06/05 11:03

本人第一次做分页,不对之处请指教,发此代码以给初学者一点参考,另外此代码比较乱,本人会整理一份比较清理的源码。

一、jsp页:

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<html>
 <head>

  <title>用户信息列表</title>
  <meta content="text/html; charset=gb2312" http-equiv="Content-Type">
  <link rel="stylesheet" type="text/css" href="form/css/input.css">

 </head>
 <body>
  <%
   int currentPage; //当前页数
   int totalPage; //总共页数
   int pageSize = 8; //每页显示行数
   String re = request.getParameter("current");
   String xx = request.getAttribute("total").toString();
   totalPage = Integer.parseInt(xx) / pageSize + 1;
   if ((re == "") || (re == null)) {
    currentPage = 1;
   } else {
    currentPage = Integer.parseInt(re);
   }
  %>
  <html:form action="/display">
   <table border=0 width="80%" cellpadding=1 cellspacing=1
    align="center" class=inputtable>
    <tr class='firstline'>
     <td>
      姓名
     </td>
     <td>
      年龄
     </td>
     <td>
      性别
     </td>
    </tr>
    <logic:notEmpty name="displayForm" property="personlist">
     <logic:iterate id="person" name="displayForm" property="personlist">
      <tr class='line'>
       <td>
        <bean:write name="person" property="personName" />
        <br>
       </td>
       <td>
        <bean:write name="person" property="personAge" />
       </td>
       <td>
        <br>
        <bean:write name="person" property="personSex" />
        <br>
       </td>
      </tr>

     </logic:iterate>
    </logic:notEmpty>
   </table>
   <table border=0 width="90%" align='center'>
    <tr align='right'>
     <td>
      第
      <%=currentPage%>
      页&nbsp;共
      
      <%=totalPage %>
      页
      
      <html:button property="firstpage" onclick='firstPage()'>第一页</html:button>
      <html:button property="prepage" onclick='prePage()'>上一页</html:button>
      <html:button property="nextpage" onclick='nextPage()'>下一页</html:button>
      <html:button property="lastpage" onclick='lastPage()'>最后一页</html:button>
      跳<html:text size="4" property="gotopage"></html:text>页
      <html:button property="goPage" onclick='goPageto()'>跳转</html:button>
     </td>
    </tr>
   </table>
   <br>
   <!--
   <html:submit/><html:cancel/>
   -->
  </html:form>
 </body>
</html>
<script language='javascript'>
 //document.all.displayForm.submit();
 function firstPage() //第一页
 {
 
  
  window.location.href="display.do?current=1";
  
 }
 function prePage() //上一页
 {
  
  var x="<%=currentPage - 1%>";
  if (x>0)
  {
   window.location.href="display.do?current=<%=currentPage - 1%>";
  
  }
  else
  {
   alert ("对不起,超出页范围!");
  }
  
 }
 function nextPage()  //下一页
 {
 
  var x="<%=currentPage + 1%>";
  var y="<%=totalPage%>";
  if (x<=y)
  {
   window.location.href="display.do?current=<%=currentPage + 1%>";
  }
  else
  {
   alert ("对不起,超出页范围!");
  }
 }
 function lastPage()  //最后一页
 {
  window.location.href="display.do?current=<%=totalPage%>";
 }
 function goPageto()  //跳转页面
 {
  
  var x="<%=totalPage%>";
  var go=document.all("gotopage").value;
  if (go<1||go>x)
  {
   alert ("对不起,超出页范围!");
  }
  else
  {
   window.location.href="display.do?current="+go;
  }
 }
</script>

二、action:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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 com.yourcompany.struts.form.DisplayForm;
import com.yourcompany.struts.form.PersonForm;

import dbUtil.DataBase;

/**
 * MyEclipse Struts Creation date: 06-06-2007
 *
 * XDoclet definition:
 *
 * @struts.action path="/display" name="displayForm" input="/form/display.jsp"
 *                scope="request" validate="true"
 */
public class DisplayAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  *
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DisplayForm displayForm = (DisplayForm) form;// TODO Auto-generated
              // method stub
  String re;
  int currentPage;  //当前页数
  int count=8;   //每页显示的记录数
  int start=0;   //开始查询记录数
  int end=0;    //结束查询记录数
  String total = null;
  
  re=request.getParameter("current");
  
  if ((re=="")||(re==null)||(re=="1"))
  {
   currentPage=1;
   start=1;
   end=count;
  }
  else
  {
   currentPage=Integer.parseInt(re);
   start=(currentPage-1)*count+(currentPage-1);
   end=currentPage*count+(currentPage-1);
  }
  String sqlStr = "select add_person_Name,add_person_Age,add_person_Sex,ro from (select add_person_Name,add_person_Age,add_person_Sex,rownum ro from addressbook where rownum<="+end+" order by sysid) where ro>="+start;
  PersonForm person = null;
  ArrayList list = new ArrayList();
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;

  try {
   DataBase db = new DataBase();
   conn = db.getConnection();
   ps = conn.prepareStatement(sqlStr);
   rs = ps.executeQuery();

   while (rs.next()) {
    person = new PersonForm();
    person.setPersonName(rs.getString("add_person_Name"));
    person.setPersonAge(rs.getInt("add_person_Age"));
    person.setPersonSex(rs.getString("add_person_Sex"));
    list.add(person);
   }
   sqlStr="select count(*) ro from addressbook";  //取总记录数
   ps = conn.prepareStatement(sqlStr);
   rs= ps.executeQuery();
   rs.next();
   total=rs.getString("ro");
   //total=rs.getInt("ro");
  
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    conn.close();
    ps.close();
    // rs.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
  request.setAttribute("total", total);
  //total = total / count + 1;  //计算总页数
  //displayForm.setTotal(total);
  displayForm.setPersonlist(list);

  return mapping.findForward("display");
 }

三、DisplayForm:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.form;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 06-06-2007
 *
 * XDoclet definition:
 * @struts.form name="displayForm"
 */
public class DisplayForm extends ActionForm {
 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 
 private ArrayList personlist;
 private int gotopage;
 private int total;
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }

 public ArrayList getPersonlist() {
  return personlist;
 }

 public void setPersonlist(ArrayList personlist) {
  this.personlist = personlist;
 }

 public int getGotopage() {
  return gotopage;
 }

 public void setGotopage(int gotopage) {
  this.gotopage = gotopage;
 }

 public int getTotal() {
  return total;
 }

 public void setTotal(int total) {
  this.total = total;
 }
}

四、PersonForm:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 06-06-2007
 *
 * XDoclet definition:
 * @struts.form name="personForm"
 */
public class PersonForm extends ActionForm {
 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 private String personName;
 private int personAge;
 private String personSex;
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }
 public int getPersonAge() {
  return personAge;
 }

 public void setPersonAge(int personAge) {
  this.personAge = personAge;
 }

 public String getPersonName() {
  return personName;
 }

 public void setPersonName(String personName) {
  this.personName = personName;
 }

 public String getPersonSex() {
  return personSex;
 }

 public void setPersonSex(String personSex) {
  this.personSex = personSex;
 }
}

原创粉丝点击