分页代码

 

1)      列表JSP页面代码清单

 

<%@ 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.getServ

erPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W 3C //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 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>

输入页码:<input type="text" value="${currentPage}" id="jumpPageBox"

size="3">

<input type="button" value="跳转"

onclick="jumpPage(document.getElementById('jumpPageBox'))">

</body>

</html>

 

2)      控制层Action代码

 

package com.yourcompany.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");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值