Struts2 实现分页

1.转自:http://www.cnblogs.com/shiyangxt/archive/2008/11/04/1316737.html
环境:MyEclipse6.5+Mysql5+struts2.0.11

实现分页用到三个类,分别为NewsDao.java(接口),NewsDaoImpl.java(实现),ListAction.java(调用方法)。

当然还有配好连接池的Mysql连接辅助类。在此不做过多介绍。

NewsDao.java

package com.sy.dao;

import java.util.List;

import com.sy.vo.News;

public interface NewsDao {

    public List<News> queryByPage(int pageSize,int pageNow);

    public int count();

}

NewsDaoImpl.java

package com.sy.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.sy.dao.NewsDao;
import com.sy.util.DataBaseConnection;
import com.sy.util.StringUtil;
import com.sy.vo.News;

public class NewsDaoImpl implements NewsDao {

        //获取分页新闻列表
    @SuppressWarnings("unchecked")
    public List<News> queryByPage(int i,int pageSize){
        List<News> newss=new ArrayList();

        PreparedStatement pstmt    = null ;
        String sql                = null ;
        ResultSet rs            = null ;
        DataBaseConnection dbc    = null ;
        dbc = new DataBaseConnection() ;

        sql = "select * from struts2new order by id asc limit " + i + "," + pageSize;
        try
        {        

            pstmt = dbc.getConnection().prepareStatement(sql);

            rs = pstmt.executeQuery() ;
            while(rs.next())
            {
                News news=new News();
                news.setId(rs.getInt("id"));
                news.setName(rs.getString("name"));
                news.setTitle(rs.getString("title"));
                news.setDate(rs.getString("date"));
                news.setEmail(rs.getString("email"));
                news.setContent(rs.getString("content"));
                i++;
                newss.add(news);
            }
            rs.close() ;
            pstmt.close() ;
        }
        catch(Exception e)
        {
            System.out.println(e) ;
        }
        finally
        {
            dbc.close();
        }
        return newss;
    }
             //查询总行数
    public int count() {
        int intRowCount = 0;//总行数
        PreparedStatement pstmt    = null ;
        String sql                = null ;
        ResultSet rs            = null ;
        DataBaseConnection dbc    = null ;
        dbc = new DataBaseConnection() ;

        sql = "select count(id) from struts2new order by id asc";
        try
        {
            pstmt = dbc.getConnection().prepareStatement(sql);
            rs = pstmt.executeQuery();
            rs.next();//游标指向第一行
            intRowCount=rs.getInt(1);//取得总行数
            rs.close() ;
            pstmt.close() ;
        }
        catch(Exception e)
        {
            System.out.println(e) ;
        }
        finally
        {
            dbc.close();
        }
        return intRowCount;
    }
}

ListAction.java

package com.sy.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.sy.dao.AdminDao;
import com.sy.dao.NewsDao;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.NewsDaoImpl;
import com.sy.vo.Admin;
import com.sy.vo.News;

public class ListAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
        int i=1;//中间变量
    private int k;//储存最大页面数
    private int pageNow=1; //页码数,初始为1
    private int pageSize = 5 ; //页面行数
    private int intRowCount;//总行数
    private int intPageCount;//总页数
    private Admin admin;
    private List<Admin> Adminss;
    private News news;
    @SuppressWarnings("unchecked")
    private List<News> Newss;

    private int id;
    private int aid;

    public News getNews() {
        return news;
    }

    public void setNews(News news) {
        this.news = news;
    }

    @SuppressWarnings("unchecked")
    public List<News> getNewss() {
        return Newss;
    }
    public void setNewss(List<News> newss) {
        Newss = newss;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public List<Admin> getAdminss() {
        return Adminss;
    }

    public void setAdminss(List<Admin> adminss) {
        Adminss = adminss;
    }
    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getIntRowCount() {
        return intRowCount;
    }

    public void setIntRowCount(int intRowCount) {
        this.intRowCount = intRowCount;
    }

    public int getIntPageCount() {
        return intPageCount;
    }

    public void setIntPageCount(int intPageCount) {
        this.intPageCount = intPageCount;
    }
    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }
@SuppressWarnings("unchecked")
    @Override
//显示新闻列表
    public String execute() throws Exception {

        NewsDao npage=new NewsDaoImpl();
        intRowCount=npage.count();
        k=(intRowCount + pageSize - 1) / pageSize;
        intPageCount = (intRowCount + pageSize - 1) / pageSize;//计算出总页数
        if(pageNow<1){
            pageNow=1;
        }

        if(pageNow > intPageCount)
             pageNow=intPageCount;
             i = (pageNow -1)*pageSize;
        NewsDao nlist=new NewsDaoImpl();
        if(null!=nlist.queryByPage(i,pageSize)){
        Newss = nlist.queryByPage(i,pageSize);
            return SUCCESS;
        }else{
            return "failure";
        }
    }
          ..
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="news" extends="struts-default">

..
<!-- 查看新闻列表 -->
        <action name="list" class="com.sy.action.ListAction">
            <result>/listNews.jsp</result>
            <result name="failure">/Showfailure.jsp</result>
        </action>

</package>
</struts>

显示页面listNews.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>My JSP ‘index.jsp‘ starting page</title>
    </head>
    <body>

..

        <center>
        共<s:property value="intRowCount"/>记录&nbsp;&nbsp;
        第<s:property value="pageNow"/>页&nbsp;&nbsp;
         <s:url id="url_pre" value="list.action">
         <s:param name="pageNow" value="pageNow-1"></s:param>
     </s:url>   

     <s:url id="url_next" value="list.action">
         <s:param name="pageNow" value="pageNow+1"></s:param>
     </s:url>
     <s:iterator value="Newss" status="status">
        <s:url id="url" value="list.action">
            <s:param name="pageNow" value="pageNow"/>
        </s:url>
     </s:iterator>
     <s:if test="pageNow==1">
    <s:a href="%{url_pre}">最前一页</s:a>
     </s:if>
     <s:else>
     <s:a href="%{url_pre}">上一页</s:a>
     </s:else>
     <s:if test="pageNow==k">
     <s:a href="%{url_next}">最后一页</s:a>
       </s:if>
       <s:else>
       <s:a href="%{url_next}">下一页</s:a>
       </s:else>
       </center>
    </body>
</html>

效果:

struts2分页导航

首页 | 上一页 1 [2] [3] [4] [5] [6] [7] [8] [9] [10] | 下一页 | 尾页  
上面效果中的 1 [2] [3] [4] [5] [6] [7] [8] [9] [10] 这个效果用struts2的bean标签可以完成。如下:

<s:bean name="org.apache.struts2.util.Counter" id="counter">
  <s:param name="first" value="1" />//循环开始 这用时要放变量来反映分页导航条的变化
  <s:param name="last" value="10" />//循环结束 这用时要放变量来反映分页导航条的变化

   <s:iterator>//循环分页导航

   <s:if test="pager.currentPage!=current-1">//不是当前页的显示效果
    <a href="<s:property value="#
URL"/>&pageNum=<s:property/>">
[<s:property/>]</a>
    </s:if>
    <s:else>//当前页的显示效果
    <font color="red"><s:property/></font>
    </s:else>
   </s:iterator>
</s:bean>

共大家参考。

3.http://blog.csdn.net/hy0231/archive/2008/12/15/3522791.aspx

1,view层

     <s:url id="url_pre" value="queryMember.action">
         <s:param name="pageCur" value="pageCur-1"></s:param>
     </s:url>
     <s:url id="url_next" value="queryMember.action">
         <s:param name="pageCur" value="pageCur+1"></s:param>
     </s:url>
     <s:a href="%{url_pre}">上一页</s:a>

     <s:iterator value="memberList" status="st">
        <s:url id="url" value="queryMember.action">
            <s:param name="pageCur" value="pageCur"/>
         </s:url>
     </s:iterator>
     <s:a href="%{url_next}">下一页</s:a>

 2,Action层
public class MemberListAction
{
 private int pageCur = 1;
 private int totalCount = 0;
 private int totalPage = 0;
 private List memberList = null;

 public String execute() throws Exception
 {
  MemberDAO dao = new MemberDAO();

/*这里是做分页的处理,有总记录数temp,总页数totalPage,当前页数pageCur

SysConstant.pageSize是一个常量,代表每页显示的记录数

 */
  //总数量
  int temp = dao.getCount(id,name);
  setTotalCount(temp);

  if((pageCur-1)*SysConstant.pageSize >= temp)
  {
   pageCur = pageCur - 1;
  }
  if(pageCur <= 0)
  {
   pageCur = 1;
  }
  if(temp == 0)
  {
   totalPage = 0;
  }
  else
  {
   totalPage =
   (int) Math.ceil((double) temp / SysConstant.pageSize);
  }
  /**/
  memberList = dao.getMember(id,name,pageCur);
  return "SUCC";
 }

 3,DAO层

这里直接写SQL。

MSQL的分页脚本:/*pageCur是当前页数,pageSize是每页显示记录数*/

select * from tmember  limit ((pageCur-1)*pageSize),(pageCur*pageSize)

ORACLE的分页脚本:

select * from (
       select a.*,rownum as a_rownum  from (
                         select * from tmember order by id
                         ) a where rownum<=(pageCur*pageSize)
            ) b where b.a_rownum>(pageCur-1)*pageSize
时间: 2024-10-29 00:18:49

Struts2 实现分页的相关文章

5、Spring+Struts2+MyBatis+分页(无代理)增删改查

1.创建如下项目结构 2.在src下的com.entity包下创建Dept.java 1 package com.entity; 2 /** 3 * 部门表 4 * @author Holly老师 5 * 6 */ 7 public class Dept { 8 private Integer deptno; //部门编号 9 private String dname; //部门名称 10 private String loc; //位置 11 12 13 public Dept() { 14

Spring+Struts2+MyBatis+分页(mybatis无代理)增删改查

1.创建如下项目结构 2.在src下的com.entity包下创建Dept.java package com.entity; /** * 部门表 * @author CHIL * */ public class Dept { private Integer deptno; //部门编号 private String dname; //部门名称 private String loc; //位置 public Dept() { } public Dept(Integer deptno, String

hibernate和struts2实现分页功能

1.DAO层接口的设计,定义一个PersonDAO接口,里面声明了两个方法: public interface PersonDAO { public List<Person> queryByPage(String hql, int offset, int pageSize); public int getAllRowCount(String hql); } 2.DAO层接口的实现类PersonDAOImpl类,将其两个方法实现出来: public class PersonDAOImpl imp

hibernate+struts2+jsp分页

Hibernate分页查询代码         /**  * 分页查询  */ public List<Ptuser> fenye(int pagenum,int num_one) { //获得session Session session = hibernateTemplate.getSessionFactory().openSession(); //开启事务 session.beginTransaction(); String str = "from ptuser";

struts2+hibernate(分页实现)

//Dao类中实现了list集合和pagetotal方法 package zjf.strhib.Dao; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class PageDao { public List page

struts2的分页标签

1.准备tld文件 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:s

struts2实现最简单的分页技术

struts2实现分页技术: 在对MemberUser类(表)中的数据进行分页:  1.在MemberUserDaoImpl 中添加 获取数据库中表的总数量的方法: public int counterByPage() { // TODO Auto-generated method stub sql="select count(*) from memberuser"; ResultSet rs =util.Query(sql); int totalCount=0; try { whil

Java Web 开发中如何进行分页

*分页: ????摘要:所用框架为:Hibernate+Spring+Struts2,以分页显示后台用户列表为例 ????1.设计实体:页面类PageBean ????????1.1 分析: ????????????当前页:currentPage(请求数据) ????????????总记录数:recordCount(查数据库:count(*)) ????????????页面大小:pageSize(配置文件设置值) ????????????本页的数据列表:recordList(查询数据库:sele

编程实践积累

1 编程经验实践,积累,总结,分享 2 3 4 博客地址:http://www.cnblogs.com/liaowanzhong/ 5 6 7 8 9 10 11 12 13 14 15 1. 大量群发邮件:购买Edm服务,大的互联网企业是和邮箱服务商签订协议(百度,腾讯,京东,阿里,csdn) 16 站内信 17 内网发短信:短信猫 18 19 2. Servlet Filter 生命周期 20 *Servlet:看配置文件中web.xml配置其启动的优先级别,即当load-on-startu