代码结构:
package com.action; import java.util.List; import java.util.Map; import com.bean.Pager; import com.bean.UserInfo; import com.dao.LendDao; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LendAction extends ActionSupport{ private int pageNow=1; //初始页为第一页 private int pageSize=3; //每页数据为4条,可调节 public String execute() throws Exception{ LendDao dao = new LendDao(); List list = dao.selectLend(pageNow, pageSize); Pager page = new Pager(pageNow, dao.selectLendSize()); Map session = ActionContext.getContext().getSession(); session.put("userinfo", list); session.put("pageinfo", page); return SUCCESS; } public int getPageNow() { return pageNow; } public void setPageNow(int pageNow) { this.pageNow = pageNow; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <table border="1" width="599"> <tr> <td valign="top"><jsp:include page="lenduserinfo.jsp"></jsp:include></td> </tr> <tr bgcolor="#E9EDF5" class="font1"> <td align="right"> <s:set name="page" value="#session.pageinfo"/> <a href="lendaction?pageNow=1">首页</a> <s:if test="#page.hasPre"> <a href="lendaction?pageNow=<s:property value="#page.pageNow-1"/>">上一页</a> </s:if> <s:else> <a href="lendaction?pageNow=1">上一页</a> </s:else> <s:if test="#page.hasNext"> <a href="lendaction?pageNow=<s:property value="#page.pageNow+1"/>">下一页</a> </s:if> <s:else> <a href="lendaction?pageNow=<s:property value="#page.totalPage"/>">下一页</a> </s:else> <a href="lendaction?pageNow=<s:property value="#page.totalPage"/>">尾页</a> </td> </tr> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
<table border="2">
<tr>
<td>userId</td>
<td>userName</td>
<td>userPassword</td>
</tr>
<s:iterator value="#session.userinfo" id="lend">
<tr>
<td><s:property value="#lend.userId" /></td>
<td><s:property value="#lend.userName" /></td>
<td><s:property value="#lend.userPassword" /></td>
</tr>
</s:iterator>
</table>
</body>
</html>