struts2.1.8.1+hibernate3.5.6Final的分页

1、CompanyDaoImpl.java

package com.xun.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xun.model.Company;
import com.xun.util.HibernateUtil;

public class CompanyDaoImpl {

public int count() {
int intRowCount = 0;//总行数
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
intRowCount = ((Number)(session.createQuery("select count(*) from Company")).uniqueResult()).intValue();

session.getTransaction().commit();
return intRowCount;
}

@SuppressWarnings("unchecked")
public List<Company> queryByPage(int i, int pageSize) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Company as c ");
q.setFirstResult(i);
q.setMaxResults(pageSize);
List<Company> companies = (List<Company>)q.list();
session.getTransaction().commit();
return companies;
}

}

2、CompanyAction.java

package com.xun.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.xun.dao.CompanyDaoImpl;
import com.xun.model.Company;
import com.xun.service.CompanyService;

public class CompanyAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
int i = 1;
private Integer id;
private Company company;
private CompanyService companyService = new CompanyService();
private List<Company> companies;

private int k;//储存最大页面数
private int pageNow=1; //页码数,初始为1
private int pageSize = 8 ; //页面行数
private int intRowCount;//总行数
private int intPageCount;//总页数

public List<Company> getCompanies() {
return companies;
}

public Company getCompany() {
return company;
}

public CompanyService getCompanyService() {
return companyService;
}

public String list() {
CompanyDaoImpl npage = new CompanyDaoImpl();
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;
CompanyDaoImpl nlist = new CompanyDaoImpl();
if(null!=nlist.queryByPage(i, pageSize)){
companies = nlist.queryByPage(i, pageSize);
return SUCCESS;
}else{
return "failure";

}
}

public void setCompanies(List<Company> companies) {
this.companies = companies;
}

public void setCompany(Company company) {
this.company = company;
}

public void setCompanyService(CompanyService companyService) {
this.companyService = companyService;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public int getK() {
return k;
}

public void setK(int k) {
this.k = k;
}

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;
}
}

3、Company_list.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="com.xun.model.*"%>
<%@ 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>

<title>公司信息列表</title>

<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<script type="text/javascript" src="js/toolbarAdmin.js"></script>

</head>
<body>
<jsp:include page="jspAssist/toolbarAdmin.jsp"></jsp:include>

<p><p><br>
<div id="cList">
<table align="center" border="1" style="border-collapse: collapse" bordercolor="#111111" width="60%">
<tr>
<td colspan="5" align="center">
<img src="images/userInfo.jpg">
</td>
</tr>
<tr bgcolor="#e4c6d0" height="30">
<th>公司名称</th>
<th>公司地址</th>
<th>联系电话</th>
<th>邮编号码</th>
<th>修改</th>
</tr>

<s:iterator value="companies" var="c">
<tr>
<td width="20%" align="center">
<a href="Company_wenJuan?id=<s:property value="#c.id"/>">
<font color="blue"><s:property value="#c.name"/></font>
</a>
</td>
<td width="25%" align="center">
<s:property value="#c.address"/>
</td>
<td width="12%" align="center">
<s:property value="#c.phone"/>
</td>
<td width="12%" align="center">
<s:property value="#c.post"/>
</td>
<td width="5%" align="center">
<a href="Company_updateInput?id=<s:property value="#c.id"/>"><font color="blue">修改</font></a>
</td>
</tr>
</s:iterator>

<tr>
<td colspan="5" align="center">
<img src="images/underBorder.jpg">
</td>
</tr>
</table>

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

<s:url id="url_next" value="Company_list">
<s:param name="pageNow" value="pageNow+1"></s:param>
</s:url>

<s:iterator value="companies" status="status">
<s:url id="url" value="Company_list">
<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>
</div>

<jsp:include page="copyright.jsp" flush="true" />
</body>
</html>

4、提交的页面用

<a id="linkB1" href="#"
onclick="javascript:document.fc.action='Company_list';document.fc.submit();">&nbsp;查询公司信息</a>

你可能感兴趣的:(hibernate3)