java 基于ssh 分页的实现
实体类
package com.neu.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* 客户信息
* */
public class CustoMer implements Serializable {
private static final long serialVersionUID = 2356810264296911904L;
/**
*组织单元
*
* */
private Integer customerId;
/**
* 姓名
*
* */
private String customerName;
/**
* 昵稱
* */
private String customerNick;
/**
* 密碼
* */
private String password;
/**
* 確認密碼
* */
private String repassword;
/**
* 郵箱
* */
private String email;
/**
* 出生日期
* */
private String customerData;
/**
* 住址
* */
private String customerAddress;
/**
* 工作單位
* */
private String workUnit;
/**
* 郵政編碼
* */
private String postcode;
/**
* 客戶類型
* */
private String customer;
private Set<Policy> pros=new HashSet<>();
public Set<Policy> getPros() {
return pros;
}
public void setPros(Set<Policy> pros) {
this.pros = pros;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerNick() {
return customerNick;
}
public void setCustomerNick(String customerNick) {
this.customerNick = customerNick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCustomerData() {
return customerData;
}
public void setCustomerData(String customerData) {
this.customerData = customerData;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public String getWorkUnit() {
return workUnit;
}
public void setWorkUnit(String workUnit) {
this.workUnit = workUnit;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
/**
* 无参数
*/
public CustoMer(){
}
/**
*有参数
*/
public CustoMer(int customerid,String CustoMerName,String CustoMerNick){
this.customerId=customerId;
this.customerNick=customerNick;
this.customerName=customerName;
}
/**
* 初始化全部属性的构造器
*/
}
在com.neu.bean 下创建PageModel
package com.neu.bean;
import java.util.List;
public class PageModel {
/**
* 总记录数
*/
private int total;
/**
* 当前页的记录集
*/
private List datas;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
}
然后dao层
*
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少条记录
* @return
*/
public PageModel findAllUser(int offset, int pagesize);
然后daoimpl层
public PageModel findAllUser(int offset, int pagesize) {
//得到总记录数
String queryCountHql = "select count(*) from CustoMer";
Query query = currentSession().createQuery(queryCountHql);
int total = ((Long)query.uniqueResult()).intValue();
List datas = currentSession().createQuery("from CustoMer")
.setFirstResult(offset)
.setMaxResults(pagesize)
.list();
System.out.println("分页"+datas);
//得到结果集
PageModel pm = new PageModel();
System.out.println("---"+pm.getTotal()+"--");
pm.setTotal(total);
pm.setDatas(datas);
return pm;
}
然后server 也是biz层
/**
* 分页查询
*/
public PageModel findAllAlarm(int offset, int pagesize);
然后serverimpl
public PageModel findAllAlarm(int offset, int pagesize) {
// TODO Auto-generated method stub
return customerdao.findAllUser(offset, pagesize);
}
最后action baseAction
package com.neu.adction;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements SessionAware {
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Map session;
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session=arg0;
}
}
然后actionimpl
public class CustoMerAction extends BaseAction {
public String pagerTaglib() {
private PageBean pagebean;
int pagesize = 10;
int offset = 0;
HttpServletRequest request = ServletActionContext.getRequest();
if (request.getParameter("pager.offset") != null)
offset = Integer.parseInt(request.getParameter("pager.offset"));
System.out.println("leixing==" + offset);
PageModel pm = customerbiz.findAllAlarm(offset, pagesize);
System.out.println(pm + "----");
request.setAttribute("pm", pm);
return "find";
}
最后是前台
<table id="brand_table" class="table table-bordered responsive">
<thead>
<tr>
<th class="center">ID</th>
<th class="center">姓名</th>
<th class="center">昵称</th>
<th class="center">邮箱</th>
<th class="center">出生日期</th>
<th class="center">住址</th>
<th class="center">工作单位</th>
<th class="center">邮政编码</th>
<th class="center">客户类型</th>
<th class="center">操作</th>
</tr>
<c:if test="${!empty pm.datas}">
<c:forEach items="${pm.datas}" var="cus">
<tr bgcolor="#EFF3F7">
<td align="center">${cus.customerId }</td>
<td align="center">${cus.customerName }</td>
<td align="center">${cus.customerNick}</td>
<td align="center">${cus.email}</td>
<td align="center">${cus.customerData}</td>
<td align="center">${cus.customerAddress}</td>
<td align="center">${cus.workUnit}</td>
<td align="center">${cus.postcode}</td>
<td align="center">${cus.customer}</td>
<td><a
href="cus_edit.action?cus.customerId=${cus.customerId}">修改</a>
<a href="cus_delete.action?cus.customerId=${cus.customerId}">删除</a></td>
</c:forEach>
</c:if>
<c:if test="${empty pm.datas}">
<tr>
<td colspan="5" align="center" bgcolor="#EFF3F7">没有相关的记录</td>
</tr>
</c:if>
</table>
<pg:pager url="cus_pagerTaglib.action" items="${pm.total}"
export="currentPageNumber=pageNumber" maxPageItems="10">
<pg:first>
<a href="${pageUrl}">首页</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 }">尾页</a>
</pg:last>
</pg:pager>
如有不懂可以加qq询问 1158219108