jsp,struts,mysql分頁_Iterator有沒屬性可以傳值?

xunmi258發表於2012-12-11

今天做了一個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>&nbsp;</td>
    	<td>&nbsp;</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>

<p class="indent">


// 在此輸入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;
	}
<p class="indent">


// 在此輸入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>
<p class="indent">


如能解決這個問題。真的十分,萬分感謝啊!

[該貼被xunmi258於2012-12-11 18:10修改過]

相關文章