数据库:
create table mess(username varchar(40) ,mname varchar(20) not null,mtext varchar(200)null, mnote varchar(1000) not null,insertime varchar(100) );
MessDao中的内容import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;/**
* Data access object (DAO) for domain model class Mess.
*
* @see dao.Mess
* @author MyEclipse Persistence Tools
*/public class MessDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(MessDAO.class); // property constants
/**
* 得到用户总数
*
* @return 用户记录总数
*/
public int getTotalCount() {
Query q = getSession().createQuery("select count(*) from mess");
List cc = q.list();
Integer a = (Integer) cc.get(0);
return a.intValue();
} /**
* 分页显示用户数据.
*
* @param currentPage
* 当前页码, 从 1 开始
* @param pageSize
* 每页显示数据量
* @return 用户数据
*/
public List findPagedAll(int currentPage, int pageSize) {
log.debug("分页查找");
try { if (currentPage == 0) {
currentPage = 1;
}
String queryString = "from mess";
Query queryObject = getSession().createQuery(queryString);
queryObject.setFirstResult((currentPage - 1) * pageSize);
queryObject.setMaxResults(pageSize);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public void save(Mess transientInstance) {
log.debug("saving Mess instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(Mess persistentInstance) {
log.debug("deleting Mess instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public List findByExample(Mess instance) {
log.debug("finding Mess instance by example");
try {
List results = getSession().createCriteria("dao.Mess").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding Mess instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Mess as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findAll() {
log.debug("finding all Mess instances");
try {
String queryString = "from Mess";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Mess merge(Mess detachedInstance) {
log.debug("merging Mess instance");
try {
Mess result = (Mess) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Mess instance) {
log.debug("attaching dirty Mess instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Mess instance) {
log.debug("attaching clean Mess instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
MessManger的内容:/**
*
*/
package manager;import java.util.List;import dao.MessDAO;
/**
* 用户管理类
*
* @author Administrator
*
*/
public class MessManager {
/** 用户管理 DAO */
private MessDAO messDAO = new MessDAO();
/**
* 得到用户总数
* @return 用户记录总数
*/
public int getTotalCount(){
return messDAO.getTotalCount();
}
/**
* 获取总页面数.
*
* @param pageSize
* 一页显示数据量
* @return 页面总数
*/
public int getTotalPage(int pageSize) {
int totalCount = messDAO.getTotalCount(); // 得到页面总数
int totalPageCount = ((totalCount + pageSize) - 1) / pageSize; return totalPageCount;
}
/**
* 分页显示用户数据.
* @param currentPage 当前页码, 从 1 开始
* @param pageSize 每页显示数据量
* @return 用户数据
*/
public List findPagedAll(int currentPage, int pageSize) {
return messDAO.findPagedAll(currentPage, pageSize);
}
}
jsp文件中的内容:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<%@page import="manager.MessManager"%>
<%-- 我们使用 JSTL 来访问数据 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%// 分析当前页码
String pageString=request.getParameter("page");//这句话在哪获得的参数"page"啊???
if(pageString == null || pageString.length() == 0) {
pageString = "1";
}
int currentPage= 0 ;
try {
currentPage = Integer.parseInt(pageString);// 当前页码
} catch(Exception e) {}if(currentPage == 0) {
currentPage = 1;
}int pageSize = 10;//每页显示的数据数
// 读取数据
MessManager manager = new MessManager();
//List users = manager.findPagedAll(currentPage, pageSize);//request.setAttribute("users",users);// 保存用户列表request.setAttribute("totalPage", manager.getTotalPage(pageSize));// 保存总页数
request.setAttribute("totalCount", manager.getTotalCount());// 保存记录总数
request.setAttribute("currentPage", currentPage);// 保存当前页码
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link href="css/main.css" rel="stylesheet">
<title>留言</title>
</head> <body>
<%@ include file="head.jsp"%>
共有:${totalCount}条留言<br>
<table width="802" border="1" cellpadding="0"
cellspacing="0" bordercolor="#FFCC33" align="center">
<tr>
<td align="center" valign="middle"><b>用户名</b></td>
<td><b>留言姓名</b></td>
<td><b>留言主题</b></td>
<td><b>留言内容</b></td>
<td><b>留言时间</b></td>
</tr>
</table>
第${currentPage}页/共${totalPage}页
<%-- 输出页面跳转代码, 分链接和静态文字两种 --%>
<c:if test="${currentPage > 1}">
[ <a href="${pageContext.request.contextPath}/messshow.jsp?page=${currentPage-1}">上一页</a> ]
</c:if>
<c:if test="${currentPage <= 1}">
[ 上一页 ]
</c:if>
<c:if test="${currentPage < totalPage}">
[ <a href="${pageContext.request.contextPath}/messshow.jsp?page=${currentPage+1}">下一页</a> ]
</c:if>
<c:if test="${currentPage >= totalPage}">
[ 下一页 ]
</c:if>
<%-- 输出 JavaScript 跳转代码 --%>
转到
<script>
// 页面跳转函数
// 参数: 包含页码的选择框(SELECT元素)
function jumpPage(select) {
var newUrl = "${pageContext.request.contextPath}/index.jsp?page=" + select.value;
//alert(newUrl);
document.location = newUrl;
}
</script> <!-- 输出 HTML SELECT 元素, 并选中当前页面编码 -->
<select onchange='jumpPage(this);'>
<c:forEach var="i" begin="1" end="${totalPage}">
<option value="${i}"
<c:if test="${currentPage == i}">
selected
</c:if> >第${i}页</option>
</c:forEach>
</select>
</body>
</html>
queryObject.setMaxResults(pageSize);
是不是这句哦,我测试没问题,但放到Struts就有问题了,不知道是怎么情况,,请问LZ你是什么数据库?