pager-taglib-2.0+struts2结合实现的分页

//实体类
public class Goods implements java.io.Serializable {
	private Integer id;
	private String goodName;
	private Integer goodType;
         //省略get/set方法
}

//DAO
public class GoodDao {
	/**
	 * 查询所有商品
	 */
	public List<Goods>loadAll(){
		Session session=HibernateSessionFactory.getSession();
		List<Goods>goodsList=session.createCriteria(Goods.class).list();
		System.out.println(goodsList.size());
		return goodsList;
	}
	/**
	 * 根据参数去加载每页数据
	 */
	public List<Goods> loadPage(int offset,int pagesize ){
		Session session=HibernateSessionFactory.getSession();
		List<Goods>goodsList=session.createCriteria(Goods.class).setFirstResult(offset).setMaxResults(pagesize).list();
		System.out.println(goodsList.size());
		return  goodsList;
	}
}


//Action
public class GoodAction extends ActionSupport {
    private GoodDao goodDao=new GoodDao();
    private List<Goods>goodsList=goodDao.loadAll();//所有商品集合
    private List<Goods>pageList=new ArrayList();//每页要显示的商品集合
    private Integer total=goodsList.size();//商品总条数
    private Integer offset;//从第几条数据开始加载
    //省略get/set方法
    /**
	 * 分页显示
	 */
	public String findPage(){
		HttpServletRequest request=ServletActionContext.getRequest();
		if(request.getParameter("pager.offset")==null){
			offset=0;
		}else{
			offset=Integer.parseInt(request.getParameter("pager.offset"));
			System.out.println("offset:"+offset);
		}
//10表示每页显示10条数据
		pageList=goodDao.loadPage(offset, 10);
		return "findPage";
	}

//struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
  <struts>
       <package name="default" extends="struts-default">
             <action name="goodAction" class="action.GoodAction">           
                   <result name="findPage">/good.jsp</result>
             </action>
       </package>
  </struts>

//goods.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib  uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>   
<%@ taglib  uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'good.jsp' starting page</title>
	<style type="text/css">
	  .goodsDiv{
	         positon:relative;
	         width:100px;
	         height:30px;
	         border:1px;
	      }	         
	</style>
  </head>
  <body>
        <s:iterator value="pageList" id="goods" status="s">
            <div class="goodsDiv"><s:property value="#goods.goodName"/></div>
        </s:iterator>
        
        <pg:pager items="${total}" url="goodAction!findPage" export="offset,currentPageNumber=pageNumber">
            <pg:index>
               <pg:first><a href="${pageUrl}"><nobr>[首页]</nobr></a></pg:first>
               <pg:prev><a href="${pageUrl}">前一页</a></pg:prev>
               <pg:pages>
                     <c:choose>
                        <c:when test="${currentPageNumber eq pageNumber}">  
			               <font color="red">${pageNumber }</font>
			            </c:when>  
			            <c:otherwise>  
			               <a href="${pageUrl}">${pageNumber}</a> 
			            </c:otherwise>  
                     </c:choose>
               </pg:pages>
               <pg:next><a href="${pageUrl}">下一页</a></pg:next>
               <pg:last><a href="${pageUrl}"><nobr>[尾页]</nobr></a></pg:last>
            </pg:index>    
        </pg:pager>
  </body>
</html>

//效果图
pager-taglib-2.0+struts2结合实现的分页

你可能感兴趣的:(java,DAO,apache,jsp,struts)