1.Action的配置:
private Integer id;
private String content;
private String username;
private int pageNow = 1; // 初始化为1,默认从第一页开始显示
private int pageSize = 10; // 每页显示5条记录
private int pageCount;// 总页数
private int rowCount;// 总记录
/**省略get和set方法*/
/** 后台获得帖子列表 */
public String getlist() {
Session s = HibernateUtil.getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String hql = "from Topic order by createTime desc";
Query query = s.createQuery(hql);
rowCount = query.list().size(); // 统计总行数
pageCount = (rowCount + pageSize - 1) * pageSize; // 计算页面总数
if (rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
} else {
pageCount = rowCount / pageSize + 1;
}
if (pageNow > pageCount) { // 如果当前页大于总页数
pageNow = 1; // 默认为一页
}
if (pageNow == 0) { // 如果当前页数为0
pageNow = 1; // 默认为一页
}
query.setFirstResult((pageNow - 1) * pageSize); // 开始页
query.setMaxResults(pageSize); // 每页最大显示数
List<Topic> list = query.list();
if (list.size() > 0) {
request.setAttribute("topiclist", list);
request.setAttribute("pageCount", pageCount);
request.setAttribute("rowCount", rowCount);
request.setAttribute("pageNow", pageNow);
} else {
request.setAttribute("message", "现在还没有人留言!");
}
return "topiclistadmin";
}
2。jsp页面
<form method="post" action="<%=basePath%>topic!getlist.action"
id="form" name="form">
<a>总共${rowCount }条留言</a> 第<a>${pageNow }</a>页 共<a>${pageCount
}</a>页
<c:if test="${pageNow>1 }">
<a
href="<%=basePath%>topic!getlist.action?pageNow=${pageNow-1 }">
上一页</a>
</c:if>
<c:if test="${pageNow< pageCount }">
<a
href="<%=basePath%>topic!getlist.action?pageNow=${pageNow+1 }">下一页
</a>
</c:if>
转到第:<input type="text" id="pageNow" name="pageNow" size="8"
value="${pageNow }" /> 页 <span><input class=buttonface
type='submit' value='GO' name='cndok' /> </span>
</form>
private Integer id;
private String content;
private String username;
private int pageNow = 1; // 初始化为1,默认从第一页开始显示
private int pageSize = 10; // 每页显示5条记录
private int pageCount;// 总页数
private int rowCount;// 总记录
/**省略get和set方法*/
/** 后台获得帖子列表 */
public String getlist() {
Session s = HibernateUtil.getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String hql = "from Topic order by createTime desc";
Query query = s.createQuery(hql);
rowCount = query.list().size(); // 统计总行数
pageCount = (rowCount + pageSize - 1) * pageSize; // 计算页面总数
if (rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
} else {
pageCount = rowCount / pageSize + 1;
}
if (pageNow > pageCount) { // 如果当前页大于总页数
pageNow = 1; // 默认为一页
}
if (pageNow == 0) { // 如果当前页数为0
pageNow = 1; // 默认为一页
}
query.setFirstResult((pageNow - 1) * pageSize); // 开始页
query.setMaxResults(pageSize); // 每页最大显示数
List<Topic> list = query.list();
if (list.size() > 0) {
request.setAttribute("topiclist", list);
request.setAttribute("pageCount", pageCount);
request.setAttribute("rowCount", rowCount);
request.setAttribute("pageNow", pageNow);
} else {
request.setAttribute("message", "现在还没有人留言!");
}
return "topiclistadmin";
}
2。jsp页面
<form method="post" action="<%=basePath%>topic!getlist.action"
id="form" name="form">
<a>总共${rowCount }条留言</a> 第<a>${pageNow }</a>页 共<a>${pageCount
}</a>页
<c:if test="${pageNow>1 }">
<a
href="<%=basePath%>topic!getlist.action?pageNow=${pageNow-1 }">
上一页</a>
</c:if>
<c:if test="${pageNow< pageCount }">
<a
href="<%=basePath%>topic!getlist.action?pageNow=${pageNow+1 }">下一页
</a>
</c:if>
转到第:<input type="text" id="pageNow" name="pageNow" size="8"
value="${pageNow }" /> 页 <span><input class=buttonface
type='submit' value='GO' name='cndok' /> </span>
</form>