struts分页

 近来了解了一下struts2.0  觉得这个分页比较有用,贴于此处:
 
环境是JDK1.6+mysql5.0+jboss4.0+struts 2.0.11

主要有三个东西:show.jsp、ShowAction.java、PageInformation.java

还需完善的地方:如果没有前一页、后一页,直接把这个链接在页面上屏蔽掉

 因为不想在Action里面有太多的链接数据库的代码,所以另外搞了一个PageInformation类,来完成数据库查询工作。

具体代码如下:

package com.ClockWise.ray.jsp;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;



public class PageInformation {

             private int pageSize;//每一页包含的条目个数

             private int totalRows;//一共有多少行

             private int totalPages;//一共有多少页

            

             private int currentPage=1;//初始化当前页为第一页

             private boolean hasPrevious = false;//是否有前页,尚未使用,有待完善

             private boolean hasNext = false;//是否有后页,尚未使用,有待完善

            

             private ArrayList<User> list = new ArrayList<User>();//存放结果的列表

             private DatabaseGeneralServices dgs;//自己写的获得connection的类,可以自己实现

             private Connection conn;          

             private PreparedStatement ps;

             private ResultSet rs;

            

             public PageInformation(){

              dgs = DatabaseGeneralServices.getInstance();

             pageSize = 20;//每页设置为20条

             totalRows = initRowCount();//得到总行数,比较粗犷,不知有什么灵巧的方法,比如select count(*)...

             totalPages =((totalRows+pageSize)-1)/pageSize;//获得总页数

             initList(1);

                                           }

             

             private void initList(int currentPage){

                 list.removeAll(list);

             conn = dgs.getConnection();

             try{

                        ps = conn.prepareStatement("Select * FROM jsptest LIMIT ?,20");

                        int temp = (currentPage-1)*20;

                        ps.setInt(1, temp);

                        rs = ps.executeQuery();

                        while (rs.next()){

                                 User user = new User();

                                 user.setId(rs.getString(1));

                                 user.setName(rs.getString(2));

                                 list.add(user);

                        }

             }catch(SQLException e){

                        e.printStackTrace();

             }finally{

                        dgs.closeConnection(rs, ps, conn);

             }

             }

             //粗犷的得到行数

             private int initRowCount(){

             conn = dgs.getConnection();

             try{

             ps = conn.prepareStatement("Select * FROM jsptest");

             rs = ps.executeQuery();

             rs.last();

             int result = rs.getRow();

             rs.first();

             return result;

             }catch(SQLException e){

                        e.printStackTrace();

             }finally{

                        dgs.closeConnection(rs, ps, conn);

             }

             return 0;

             }

            

             //页面调数据的时候,重新发出查询,初始化结果列表

                   public ArrayList<User> getList(int cp) {

                            initList(cp);

                            ArrayList<User> temp = new ArrayList<User>();

                            for(int i =0;i<20;i++){

                                     temp.add(list.get(i));

                            }

                            return temp;

                   }

        //other setters and getters

                   ……



紧接着是Action代码

package com.ClockWise.ray.jsp;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class ShowAction extends ActionSupport {

         private int totalPages;

         private boolean hasPre;

         private boolean hasNext;

         private int currentPage=1;

         private ArrayList<User> list;

         //以上几个和PageInformation里面的一一对应

         private String username ="ray";

         private ArrayList list2= new ArrayList();//这个list用来存放下面的页码起始位置

         private PageInformation pi;

        

         public String execute(){

                   init(currentPage);

                   return SUCCESS;

         }

        

         private void init(int currentPage){

                   pi = new PageInformation();

                   this.setTotalPages(pi.getTotalPages());

                   this.setHasPre(pi.isHasPrevious());

                   this.setHasNext(pi.isHasNext());

                   this.setList(pi.getList(currentPage));

                  

                   for(int i =currentPage;i<=currentPage+20;i++){

                            Current c = new Current();

                            c.setCurrentPage(i);

                            list2.add(c);

                   }

         }

        

         public ArrayList<User> getList() {

                   this.setList(pi.getList(currentPage));

                   return list;

         }

//other setters and getters

…….



最后是JSP代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

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

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<s:form action="ShowAction" method="GET">

   <h1>Welcome <s:property value="username"/></h1><BR>

   <h1>CurrentPage <s:property value="currentPage"/></h1>

  

   <!--show items of this page-->

   <s:iterator value="list" status="status">

        <s:property value="id"/>

        <s:property value="name"/>

        <BR>

   </s:iterator>

            

   <!--define the url of the previous page and next page-->

     <s:url id="url_pre" value="ShowAction.action">

         <s:param name="currentPage" value="currentPage-1"></s:param>

     </s:url>

     <s:url id="url_next" value="ShowAction.action">

         <s:param name="currentPage" value="currentPage+1"></s:param>

     </s:url> 

    

   <!-- use url defined above -->    

   <s:a href="%{url_pre}">Pre</s:a>

  

   <s:iterator value="list2" status="status">

      <s:url id="url" value="ShowAction.action">

      <!-- pass the currentPage parameter -->

         <s:param name="currentPage" value="currentPage"></s:param>

      </s:url>

      <s:a href="%{url}"><s:property value="currentPage"/> </s:a>

   </s:iterator>

   <s:a href="%{url_next}">Next</s:a>

</s:form>  

</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值