一 配置PageBean的java类,即分页封装的类。(泛型)
那么,配置一个分页查询的实体类,需要配置什么私有变量?显然,当前处于多少页是需要的,总页数是需要的;总记录数是需要的,每页显示多少个记录是需要的;记录内容(集合)也是需要的。
package com.ssh.domain;
import java.util.List;
/*
* 分页封装的java类
*
*/
public class PageBean<Product> {
private int countTotal;//总记录数
private int pageSize;//每页显示的记录数
private int pageTotal;//总页数
private int currentPage;//当前页数
private List<Product> plist;//返回的数据
public int getCountTotal() {
return countTotal;
}
public void setCountTotal(int countTotal) {
this.countTotal = countTotal;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageTotal() {
return pageTotal;
}
public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public List<Product> getPlist() {
return plist;
}
public void setPlist(List<Product> plist) {
this.plist = plist;
}
}
二 显示当前从数据库返回的集合记录的代码逻辑:
前几页返回的记录总数+1为初始记录,显示每页显示多少个记录的个数的集合记录。
因此,为了配置Dao层,调用DetachedCriteria的findByCriteria方法,需要的两个量为:1(当前页数-1)*每页显示的个数,2 每页显示的个数。
1 Action层的配置:
原理很简单,把jsp传入的值推进值栈里,于是自然地导入了pageBean。
package com.ssh.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ssh.domain.PageBean;
import com.ssh.domain.Product;
import com.ssh.service.ProductService;
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
private Product product=new Product();
private Integer currentPage;
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
@Override
public Product getModel() {
// TODO Auto-generated method stub
return product;
}
private ProductService productService;
public void setProductService(ProductService productService) {
this.productService = productService;
}
public Product findById(int pid){
product=productService.findById(pid);
// System.out.println(product.getPname()+product.getPprice());
return product;
}
public String save(){
System.out.println("Action");
productService.Save(product);
return "saveSuccess";
}
public String delete(){
product=findById(product.getPid());
//System.out.println("ActionDelete");
productService.Delete(product);
return "deleteSuccess";
}
public String update(){
// product=findById(product.getPid());
productService.update(product);
return "updateSuccess";
}
public String findAll( ){
PageBean<Product> pageBean=productService.findByPage(currentPage);
//将pageBean存入到值栈中
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
}
2 Service层的设置:
package com.ssh.service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.dao.ProductDao;
import com.ssh.domain.PageBean;
import com.ssh.domain.Product;
@Transactional
public class ProductService {
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void Save(Product product){
System.out.println("Service");
productDao.Save(product);
}
public void Delete(Product product) {
productDao.Delete(product);
}
public void update(Product product) {
productDao.update(product);
}
public Product findById(int pid) {
Product product=productDao.findById(pid);
return product;
}
public PageBean<Product> findByPage(Integer currentPage) {
PageBean<Product> pageBean=new PageBean<Product>();
/*if(currentPage==null){
currentPage=1;
}*/
//封装当前页数
pageBean.setCurrentPage(currentPage);
//封装总记录数
System.out.println(currentPage);
int countTotal=productDao.findAllAcount();
pageBean.setCountTotal(countTotal);
//封装每页记录数
int pageSize=4;
pageBean.setPageSize(pageSize);
//封装总页数
Double iniPageTotal=Math.ceil(countTotal/pageSize);
int pageTotal=iniPageTotal.intValue();
pageBean.setPageTotal(pageTotal);
//封装每页查询返回的显示数据,配合dao中的DetachedCriteria类完成分页查询的返回
int begin=pageSize*(currentPage-1);
List<Product> plist=productDao.findByPage(begin, pageSize);
pageBean.setPlist(plist);
return pageBean;
}
}
三 需要在DAO层配置的:
总记录数的查询;
封装每页显示的数据的相关查询
注:使用DetachedCriteria的findByCriteria方法处理分页查询有天然优势。
package com.ssh.dao;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.*;
import com.ssh.domain.Product;
public class ProductDao extends HibernateDaoSupport{
public void Save(Product product){
System.out.println("Dao");
this.getHibernateTemplate().save(product);
}
public void Delete(Product product) {
this.getHibernateTemplate().delete(product);
}
public void update(Product product) {
this.getHibernateTemplate().update(product);
}
public Product findById(int pid) {
Product product=this.getHibernateTemplate().get(Product.class, pid);
return product;
}
public int findAllAcount() {
String hql="select count(*) from product";
// List<Long> plist=(List<Long>) this.getHibernateTemplate().find(hql);
// if(plist.size()>0){
return 16;
// } else{
// return 16;
// }
}
public List<Product> findByPage(int begin, int pageSize) {
DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
List<Product> plist=(List<Product>) this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
return plist;
}
}
四 jsp页面使用s:iterator进行迭代,用<s:property value="">显示查询的值。
注:struts2中的#相当于ActionContext.getContext()
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.table1{
border:1px solid #ddd;
width:900px;
}
thead{
background-color:lightblue;
}
</style>
<title>商品详情</title>
</head>
<body>
<table border="0" width="900px">
<tr>
<td align="center" style="font-size:24px; color:#666"> 部门管理</td>
</tr>
<tr>
<td align="right" > <a href="addDepartment.html">添加</a></td>
</tr>
</table>
<br/>
<table cellspacing="0" border="1" class="table1">
<thead>
<tr><th width="450">部门名称</th><th width="450">编辑</th></tr>
</thead>
<tbody>
<s:iterator value="plist" var="p">
<tr>
<td align="center"><s:property value="#p.pname"/></td>
</tr>
<%-- <tr>
<td align="center"><s:property value="#p.pname"/></td>
</tr>
<tr>
<td align="center"><s:property value="#p.pprice"/></td>
</tr> --%>
</s:iterator>
</tbody>
</table>
<br/>
<table border="0" cellspacing="0" cellpadding="0" width="900px">
<tr>
<td align="right">
<span>第<s:property value="currentPage"/>/<s:property value="pageTotal"/>页</span>
<span>
<%-- <s:if test="currentPage!=1"> --%>
<a href="${pageContext.request.contextPath}/product_findAll.action?currentPage=1">[首页]</a>
<a href="${pageContext.request.contextPath}/product_findAll.action?currentPage=<s:property value="currentPage-1"/>">[上一页]</a>
<%-- </s:if> --%>
<%-- <s:if test="currentPage!=pageTotal"> --%>
<a href="${pageContext.request.contextPath}/product_findAll.action?currentPage=<s:property value="currentPage+1"/>">[下一页]</a>
<a href="${pageContext.request.contextPath}/product_findAll.action?currentPage=<s:property value="pageTotal"/>">[尾页]</a>
<%-- </s:if> --%>
</span>
</td>
</tr>
</table>
</body>
</html>