Hibernate分页应用层和jsp页面技术

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.StrutsLoginDemo.struts.action;

import java.util.List;
import javax.servlet.http.*;
import manager.UserManager;
import org.apache.struts.action.*;
public class ListAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 分析当前页码
String pageString=request.getParameter("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 = 3;//每页显示的数据数
//读取数据
UserManager manager = new UserManager();
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);// 保存当前页码
return mapping.findForward("display");
}
}

 

 

 

package manager;
import java.util.List;
/**
* 用户业务管理类
* @author BeanSoft
*/
public class UserManager {
/** 用户管理DAO */
private dao.StudentDAO dao = new dao.StudentDAO();
/**
* 得到用户总数
* @return 用户记录总数
*/
public int getTotalCount(){
return dao.getTotalCount();
}
/**
* 获取总页面数.
*
* @param pageSize
* 一页显示数据量
* @return 页面总数
*/
public int getTotalPage(int pageSize) {
int totalCount = getTotalCount();
// 得到页面总数
int totalPageCount = ((totalCount + pageSize) - 1) / pageSize;
return totalPageCount;
}
/**
* 分页显示数据.
* @param currentPage 当前页码, 从 1 开始
* @param pageSize 每页显示数据量
* @return 分页后的数据列表- List<Student>
*/
public List findPagedAll(int currentPage, int pageSize) {
return dao.findPagedAll(currentPage, pageSize);
}
}

 

<%@ page language="java" import="manager.*,java.util.*"
pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<%-- 我们使用 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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户列表页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<style>
/* 给链接加入鼠标移过变色和去除下划线功能 */
a:hover {color:red;text-decoration:none}
</style>
</head>
<body><b>用户列表页面</b><br>
<%-- 输出用户列表 --%><br>
<table width="80%" border="1" cellpadding="0"
style="border-collapse: collapse; " bordercolor="#000000">
<tr>
<td><b>用户ID</b></td>
<td><b>用户名</b></td>
<td><b>操作</b></td>
</tr>
<c:forEach items="${users}" var="user" >
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td><a href="edit.do?id=${user.id}">修改</a></td>
</tr>
</c:forEach>
</table> 共${totalCount}个用户
第${currentPage}页/共${totalPage}页
<%-- 输出页面跳转代码, 分链接和静态文字两种 --%>
<c:if test="${currentPage > 1}">
[ <a href="${pageContext.request.contextPath}/list.do?page=${currentPage-1}">上一页</a> ]
</c:if>

<c:if test="${currentPage <= 1}">
[ 上一页 ]
</c:if>
<c:if test="${currentPage < totalPage}">
[ <a href="${pageContext.request.contextPath}/list.do?page=${currentPage+1}">下一页</a> ]
</c:if>
<c:if test="${currentPage >= totalPage}">[ 下一页 ]</c:if>
<%-- 输出 JavaScript 跳转代码 --%>
<script>
// 页面跳转函数
// 参数: 包含页码的表单元素,例如输入框,下拉框等
function jumpPage(input) {
// 页码相同就不做跳转
if(input.value == ${currentPage}) {
return;
}
var newUrl = "${pageContext.request.contextPath}/list.do?page=" +
input.value;
document.location = newUrl;
}
</script>
转到
<!-- 输出 HTML SELECT 元素, 并选中当前页面编码 -->
<select οnchange='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>
输入页码:<input type="text" value="${currentPage}" id="jumpPageBox"
size="3">
<input type="button" value="跳转"
οnclick="jumpPage(document.getElementById('jumpPageBox'))">
</body>
</html>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值