今天做了一个jsp+struts+mysql分页,但pageNow老是设置不了。。。只能是在自己写的分页方法中定义pageNow值,灵活设置不了。
跪求各位分页高手帮忙啊!先附上代码:
// 在此输入java代码 viewTeams.jsp【显示层】 <%@[author]page[/author] import="bean.Team"%> <%@ [author]page[/author] language="java" import="java.util.*,java.sql.*,business.TeamBusiness" contentType="text/html; charset=utf-8" [author]page[/author]Encoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%
//定义四个分页会用到的变量 int [author]page[/author]Now = 2;
//接受用户希望显示的页数([author]page[/author]Now) String s_[author]page[/author]Now = request.getParameter("[author]page[/author]Now");
if(s_[author]page[/author]Now != null) { //接收到[author]page[/author]Now [author]page[/author]Now = Integer.parseInt(s_[author]page[/author]Now); } //调用UserBeanCL的方法(创建一个UserBeanCL的实例,然后调用它的某个方法),完成分页显示 //TeamBusiness.getUserByPage(6); //显示 %>
<s:form > <table border="1"> <tr> <td>ID值</td> <td>组名</td> <td>组长</td> <td>口号</td> <td> </td> <td> </td> </tr> <s:iterator value="teams"> [b][/b] <tr> <td><s:property value="id" /></td> <td><s:property value="name"/></td> <td><s:property value="leader"/></td> <td><s:property value="slogan"/></td> <td><a href="modifyTeam.action?team.id=<s:property value="id"/>">修改</a></td> <td><a href="deleTeam.action?team.id=<s:property value='id'/>">删除</a></td> </tr> </s:iterator> <tr> <td colson="7" style="text-align:right;"><a href="addTeam.jsp" >返回添加页面</a></td> </tr> </table> <% //上一页 if([author]page[/author]Now != 1) { out.println("<a href=viewTeam.action?[author]page[/author]Now="+([author]page[/author]Now-1)+">上一页</a>"); } //得到[author]page[/author]Count int [author]page[/author]Count = (int)TeamBusiness.getPageCount(); //显示超链接 for(int i=1;i<=[author]page[/author]Count;i++) { out.println("<a href=viewTeam.action?[author]page[/author]Now="+i+">["+i+"]</a>"); } //下一页 if([author]page[/author]Now != [author]page[/author]Count) { out.println("<a href=viewTeam.action?[author]page[/author]Now="+([author]page[/author]Now+1)+">下一页</a>"); } %> 要跳转到<input type="text" size="2" name="[author]page[/author]Now" />页 <a href=viewTeam.action?[author]page[/author]Now="+([author]page[/author]Now)+">跳转</a> </s:form>
</body> </html>
|
// 在此输入java代码【TeamBusiness.java(业务处理层)】 public class TeamBusiness { private static int [author]page[/author]Size = 3; private static int rowCount = 0;// 该值从数据库查询 private static int [author]page[/author]Count;// 该值通过[author]page[/author]Size和rowCount得出 // 返回分页的总页数 public static int getPageCount() { Connection cn = null; PreparedStatement pst = null;
ResultSet rs = null;
try {
// 得到链接 cn = DataSource.getConnection();
// 计算[author]page[/author]Count pst = cn.prepareStatement("select count(*) from team"); rs = pst.executeQuery();
// 一定要记得next if (rs.next()) { rowCount = rs.getInt(1); }
// 计算[author]page[/author]Count.算法很多 if (rowCount % [author]page[/author]Size == 0) { [author]page[/author]Count = rowCount / [author]page[/author]Size; System.out.println("" + [author]page[/author]Count); } else { [author]page[/author]Count = rowCount / [author]page[/author]Size + 1; System.out.println("" + [author]page[/author]Count); }
} catch (Exception er) { er.printStackTrace(); } finally { try { rs.close(); cn.close(); pst.close(); } catch (Exception e) { e.printStackTrace(); } } return [author]page[/author]Count; }
// 得到用户需要显示的用户信息(分页) public static ArrayList<Team> getUserByPage(int [author]page[/author]Now) { ArrayList<Team> teams = new ArrayList<Team>();
Connection cn = null; PreparedStatement pst = null; //[author]page[/author]Now=3; int [author]page[/author]now=(int)[author]page[/author]Now; System.out.println("" + [author]page[/author]now); ResultSet rs = null;
try { cn = DataSource.getConnection(); pst = cn.prepareStatement("select * from team limit "+[author]page[/author]now+","+[author]page[/author]Size+""); System.out.println("fenye方法执行到了!"); rs = pst.executeQuery();
while (rs.next()) { Team team = new Team();
team.setId(rs.getInt("id")); team.setName(rs.getString("name")); team.setLeader(rs.getString("leader")); team.setSlogan(rs.getString("slogan"));
// 将team放入到arraylist中 teams.add(team); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); cn.close(); pst.close(); } catch (Exception e) { e.printStackTrace(); } } return teams; }
|
// 在此输入java代码
【struts.xml】 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="false" /> <package name="team" extends="struts-default" namespace="/"> <action name="addTeam" class="action.AddTeam"> <result name="success" type="redirect">viewTeam.action</result> <result name="error">/addFailed.jsp</result> </action> <action name="viewTeam" class="action.ViewTeam"> <result name="success">/viewTeams.jsp</result> <result name="error">/addSuccess.jsp</result> </action> <action name="modifyTeam" class="action.ModifyTeam"> <result name="success">/modifyTeam.jsp</result> <result name="error">/index.jsp</result> </action> <action name="modifyTeam1" class="action.ModifyTeam1"> <result name="success">/success.jsp</result> <result name="error">/index.jsp</result> </action> <action name="deleTeam" class="action.DeleTeam"> <result name="success">/success.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>
|
如能解决这个问题。真的十分,万分感谢啊!
[该贴被xunmi258于2012-12-11 18:10修改过]