`
115893520
  • 浏览: 140414 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

struts2+hibernate+spring分页方法

阅读更多
  
 import java.util.List; public interface Pagination { public boolean isFirst();//判断是否为第一页 public boolean isLast();//判断是否为最后一页 public boolean hasNext();//判断是否还有下一页 public boolean hasPrevious();//判断是否还有上一页 public int getMaxElements();//还回分页总记录数 public int getMaxPages();//还回总页数 public int getNext();//获得下一页的页面 public int getPrevious();//获得上一页的页面 public int getPageSize();//还回分页大小 public int getPageNumber();//还回当前页的页面 public List<Object> getList();//还回当前页的记录数据 public void setPageSize(int pageSize);//设置分页大小 public void setPageNumber(int pageNumber);//设置跳转的页面的页码 }
  Pagination 接口实现
public class PaginationImpl  implements Pagination {
	private int pageSize = 20;

	private int pageNumber = 1;

	private int maxPages;

	private int maxElements;

	private List<Object> list;

	public PaginationImpl() {
	}


	public PaginationImpl(List<Object> list, int maxElements, int pageSize, int pageNumber) {
		this.pageSize = pageSize;
        this.list=list;
        this.maxElements=maxElements;
		init();
		this.setPageNumber(pageNumber);
	}

	private void init() {
		setMaxPages();
	}

	private void setMaxPages() {
		if (maxElements != 0 && (maxElements % pageSize == 0)) {
			maxPages = maxElements / pageSize;
		} else {
			maxPages = maxElements / pageSize + 1;
		}
	}

	public List<Object> getList() {
        return this.list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public int getMaxElements() {
		return maxElements;
	}

	public int getMaxPages() {
		return maxPages;
	}

	public int getNext() {
		if (pageNumber + 1 >= this.getMaxPages()) {
			return getMaxPages();
		}
		return pageNumber + 1;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPrevious() {
		if (pageNumber - 1 <= 1) {
			return 1;
		} else {
			return pageNumber - 1;
		}
	}

	public boolean hasNext() {
		return pageNumber < this.getMaxPages();
	}

	public boolean hasPrevious() {
		return pageNumber > 1;
	}

	public boolean isFirst() {
		return pageNumber == 1;
	}

	public boolean isLast() {
		return pageNumber >= this.getMaxPages();
	}

	public void setPageNumber(int pageNumber) {
		if (pageNumber > maxPages) {
			this.pageNumber = maxPages;
		} else if (pageNumber < 1) {
			this.pageNumber = 1;
		} else {
			this.pageNumber = pageNumber;
		}
	}

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

	public void setMaxElements(int maxElements) {
		this.maxElements = maxElements;
	}


}
 
public interface IPaginationDao {
	public List<Object> listByPage(String hql,int pageSize, int pageNumber);
	public int listCount(String hql);
}
    
 IPaginationDao 接口实现
public class PaginationDaoImpl extends HibernateDaoSupport implements IPaginationDao{

public List<Object> listByPage(final String hql, final int pageSize, final int pageNumber) {
		
		return  (List<Object>)this.getHibernateTemplate().executeFind( new  HibernateCallback(){
            public  Object doInHibernate(Session session)  throws  SQLException,HibernateException {
              Query q  =  session.createQuery(hql);
              q.setFirstResult((pageNumber - 1) * pageSize);
              q.setMaxResults(pageSize);	           
              List<Object> list = (List<Object>) q.list();  
              return  list;  
              }
       });
	}

	public int listCount(String hql) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find(hql).size();
	}

}
 服务接口
public interface IPaginationServ { public Pagination getProductByPage(String hql,int pageSize, int pageNumber);}
 
IPaginationServ  接口实现
public class PaginationServImpl implements IPaginationServ{
	 private IPaginationDao pagingDao;
	public Pagination getProductByPage(String hql, int pageSize, int pageNumber) {
	         int total =pagingDao.listCount(hql);
	         List<Object> list=pagingDao.listByPage(hql, pageSize, pageNumber);
	         Pagination paging=new PaginationImpl(list,total,pageSize,pageNumber);
	         return paging;
	}
	public IPaginationDao getPagingDao() {
		return pagingDao;
	}
	public void setPagingDao(IPaginationDao pagingDao) {
		this.pagingDao = pagingDao;
	}

}
 配置spring文件
<bean id="pagingDao"
		class="××××.dao.impl.PaginationDaoImpl">
		<property name="sessionFactory" >
		    <ref bean="sessionFactory"/>
		</property>
	</bean>
<bean id="pagingServ"
  class="com.cnkf.wushi.service.impl.PaginationServImpl">
  <property name="pagingDao" >
      <ref bean="pagingDao"/>
  </property>
 </bean>
 添加页面
 
<%@taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<a href="findAllDisplay.action"><h1>displayTag分页</h1></a>
			<s:iterator value="page.list" id="pr">
				<table width="300" border="1" bordercolor="000000"
					style="table-layout: fixed; word-break: break-all">
					<tr>
						<td width="100" bordercolor="ffffff">
							编号:
						</td>
						<td width="500" bordercolor="ffffff">
							<s:property value="id" />
						</td>
					</tr>
					<tr>
						<td bordercolor="ffffff">
							名称:
						</td>
						<td bordercolor="ffffff">
							<s:property value="name" />
						</td>
					</tr>
					<tr>
						<td bordercolor="ffffff">
							价格:
						</td>
						<td bordercolor="ffffff">
							<s:property value="price" />
						</td>
					</tr>
					<tr>
						<td bordercolor="ffffff">
							类型:
						</td>
						<td bordercolor="ffffff">
							<s:property value="type" />
						</td>
					</tr>
				</table>
				<br>
			</s:iterator>
			<pg:pager url="findAllPg.action"
				items="${page.maxElements}"
				maxPageItems="${pageSize}" maxIndexPages="5">
				<pg:index>
					<pg:first unless="current">
						<a href="${pageUrl}&pageNumber=${pageNumber}">首页</a>
					</pg:first>
					<pg:prev>
						<a href="${pageUrl}&pageNumber=${pageNumber}">上一页(${pageNumber})</a>
					</pg:prev>
					<pg:pages>
						<a href="${pageUrl}&pageNumber=${pageNumber}">${pageNumber}</a>
					</pg:pages>
					<pg:next>
						<a href="${pageUrl}&pageNumber=${pageNumber}">下一页(${pageNumber})</a>
					</pg:next>
					<pg:last unless="current">
						<a href="${pageUrl}&pageNumber=${pageNumber}">尾页</a>
					</pg:last>
				</pg:index>
			</pg:pager>
</center>
 
<%@taglib uri="http://displaytag.sf.net" prefix="display"%>
<center>
                 <a href="findAllPg.action"><h1>Pager分页</h1></a>
			<display:table name="page.list" id="dt" export="true" class="" partialList="true" size="page.maxElements" pagesize="10"
				requestURI="findAllDisplay.action">
				<display:column sortable="true" property="id" title="编号">
				</display:column>

				<display:column property="name" title="名称">
				</display:column>

				<display:column property="price" title="价格">
				</display:column>

				<display:column property="type" title="类型">
				</display:column>

				<display:column title="操作">
					<a href="#">编辑</a>
					<a href="#">删除</a>
				</display:column>
			</display:table>

</center>
 
 添加action
 
public class ProductAction extends ActionSupport {
	private static final long serialVersionUID = 3953659778944144652L;

	private IProductServ productServ;

	private IPaginationServ pagingServ;

	private Product pr;

	private List<Product> list;

	private Pagination page;

	private String pageNumber = "1";

	private int pageSize = 10;

	public String findAllDisplay() {
		String hql = "from Product p";
	// 这是displaytag获得pageNumber的方法,dt是displaytag 的id属性相对应的。比较特殊
		String paramName = (new ParamEncoder("dt")
				.encodeParameterName(TableTagParameters.PARAMETER_PAGE));
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			Integer.parseInt(request.getParameter(paramName));
			pageNumber=request.getParameter(paramName);
		} catch (Exception e) {
			pageNumber = "1";
		}
		page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
		return SUCCESS;
	}
	public String findAllPg() {
		String hql = "from Product p";
		try {
			Integer.parseInt(pageNumber);
		} catch (Exception e) {
			pageNumber = "1";
		}
		page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
		return SUCCESS;
	}
 
配置struts.xnl文件
<action name="findAllDisplay" class="com.cnkf.template.action.ProductAction" method="findAllDisplay">
			 <result >/welcomeDisplay.jsp</result>
		</action>
		<action name="findAllPg" class="com.cnkf.template.action.ProductAction" method="findAllPg">
			 <result >/welcomepg.jsp</result>
		</action>
 
最后添加上displaytag 的jar包,还有img,css文件
pager的jar包添加

 

 

5
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics