62,610
社区成员
发帖
与我相关
我的任务
分享
package com.mine.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.mine.server.Service;
public class User_showAction 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;//每页显示的数据数
// 读取数据
Service manager = new Service();
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");
}
}
<%@ page language="java" import="com.mine.server.*,java.util.*"
pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
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>
<table width="100%" border="0">
</table>
<table width="80%" border="1" cellpadding="0"
style="border-collapse: collapse; " bordercolor="#000000">
<tr>
<td>用户名</td>
<td>密码</td>
<td>地址</td>
<td>电话</td>
<td>邮箱</td>
<td>性别</td>
<td>爱好</td>
<td>操作</td>
</tr>
<logic:iterate name="users" id="user">
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.address}</td>
<td>${user.tel}</td>
<td>${user.email}</td>
<td>${user.sex}</td>
<td>${user.enjoy}</td>
<td>${user.content}</td>
<td><a href="edit.html?id=${user.id}">修改</a></td>
</tr>
</logic:iterate>
</table> 共${totalCount}个用户
第${currentPage}页/共${totalPage}页
<c:if test="${currentPage > 1}">
[ <a
href="${pageContext.request.contextPath}/user_show.html?page=${currentPage-1
}">上一页</a> ]
</c:if>
<c:if test="${currentPage <= 1}">
[ 上一页 ]
</c:if>
<c:if test="${currentPage < totalPage}">
[ <a
href="${pageContext.request.contextPath}/user_show.html?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}/user_show.html?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>