標籤:
有了之前幾篇文章關於Strus2+json結合的基礎,自己也嘗試著寫分頁表格顯示資料的代碼。也不枉花了些時間,終於搞定了,“紙上得來終覺淺,覺知此事要躬行”,哈哈,不多說,具體如下:
1. 前端頁面 --- html代碼+css代碼+js代碼
html代碼:
<body class="body"> <table class="tab"> <tr class="tr_0"> <td class="td_1">序號</td> <td class="td_2">名稱</td> <td class="td_3">癥狀</td> <td class="td_4">病因</td> <td class="td_5">防治措施</td> </tr> </table> <div class="pageBtn"> 第<span class="currentPage"> </span>頁,共<span class="totalPage"></span>頁 <span id="firstPage" class="pageClick">首頁</span> <span id="prev" class="pageClick">上一頁</span> <span id="next" class="pageClick">下一頁</span> <span id="lastPage" class="pageClick">最後一頁</span> <span id="jump" class="pageClick">跳至</span> 第<input type="text" class="page_input">頁 </div> </body>
css代碼:
<style type="text/css">.body{margin:50px;}.tab{font-size:13px;border-collapse:collapse; }.tab td{border:1px solid #211;text-align:center;}.td_1{width:28px;}.td_2{width:92px;}.td_3,.area_1{width:150px;}.td_4,.area_2{width:150px;}.td_5,.area_3{width:180px;}.tab textarea{overflow:auto;border:0;font-size:13px;}.pageBtn{font-size:14px;margin-top:20px;}.pageBtn span{cursor:pointer;}.pageClick{padding-left:10px;padding-right:10px;}.page_input{margin-left:5px;margin-right:5px;width:45px;text-align:center;}</style>
javascript代碼:
<script type="text/javascript" src="js/jquery1.9.1.js"></script><script charset="gb2312" type="text/javascript" > $(function () {var total = 0; //資料表中的記錄總數var pageSize = 4; //一個頁面顯示4條記錄var curPage = 1; //當前頁面變數$.post("page/countAll",null, function (data) { total = data.count; $(".currentPage").text("1"); showPage(1,4);// 開啟頁面預設顯示第一頁 var pageTotal = Math.ceil(total/pageSize); //總頁數 $(".totalPage").text(pageTotal); //總頁數資訊寫到前台頁面 $("#next").click(function() { if (curPage == pageTotal) { alert("這是最後一頁"); curPage = pageTotal; } else { curPage++; gotoPage(curPage,total); } }); $("#prev").click(function() { if (curPage == 1) { alert("這是第一頁"); curPage = 1; } else { curPage--; gotoPage(curPage,total); } }); $("#firstPage").click(function() { curPage = 1; gotoPage(curPage,total); }); $("#lastPage").click(function() { curPage = pageTotal; gotoPage(curPage,total); }); $("#jump").click(function(){ if($(".page_input").val() <= pageTotal && $(".page_input").val() != ''){ curPage = $(".page_input").val(); gotoPage(curPage,total); }else{ alert("您輸入的頁碼有誤!"); $(".page_input").val('').focus(); } }); });});function showPage(curPage,total){// 表格方式顯示擷取的資料$.ajax({url: "page/show",data: { "PageIndex": curPage},type: "post",dataType: "json",success: function (data) { var result = eval(data.result); $(".tab tr:gt(0)").remove(); $.each(result, function(i,o) { $(".tab").append($("<tr><td>" + o.id+ "</td><td>" +o.name + "</td><td><textarea class='area_1'>"+ o.symptom + "</textarea></td><td><textarea class='area_2'>"+ o.reason + "</textarea></td><td><textarea class='area_3'>"+ o.cure + "</textarea></td></tr>")); }); }});}function gotoPage(curPage,total) { $(".currentPage").text(curPage); showPage(curPage,total); } </script>
2. strust.xml配置資訊:
<struts> <constant name="struts.devMode" value="true" /><package name="ajaxPage" extends="json-default" namespace="/page"><action name="show" class="testaction.PageJsonAjaxAction" method="showPage"><result name="success" type="json"></result></action><action name="countAll" class="testaction.PageJsonAjaxAction" method="countAll"><result name="success" type="json"></result></action></package></struts>
3. 後台action類 --- PageJsonAjaxAction.java
相關的jar包如下,我的jar包全放在/WebRoot/WEB-INF/lib下的
package testaction;import java.sql.Connection;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;import sql.SQLConnection;import com.opensymphony.xwork2.ActionSupport;/**@author 小霞 * 2014/12/23 * */public class PageJsonAjaxAction extends ActionSupport{private static final long serialVersionUID = 1L;private int pageIndex;//接收頁面傳來的第幾頁的數字private int count;//儲存記錄總數private String result;//作為響應結果以json格式傳遞到前台頁面/** * 三個變數的getter及setter,在沒有任何設定的情況下,該類下的所有getter方法的傳回值將被包含 * 在返回給用戶端的JSON字串中。要剔除不需要包含的屬性,在類結構結構中需要在getter方法上使 * 用@JSON(serialize=false)進行註解,當然在不影響其他業務的時候也可以直接去掉這個getter方法。 * */public String getResult() {return result;}public void setResult(String result) {this.result = result;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getPageIndex() {return pageIndex;}public void setPageIndex(int pageIndex) {this.pageIndex = pageIndex;}/**查詢資料表全部的記錄個數*/public String countAll() throws SQLException{//擷取資料表的總記錄數System.out.println("Page count begins....");Connection con = new SQLConnection().getConnection();String sql = "select * from desease";JSONObject jo = new JSONObject();int c = 0;try {Statement st = con.createStatement();ResultSet rs = st.executeQuery(sql);while(rs.next()){c++;}count = c;System.out.println("the count is "+count);jo.element("count",c);result = jo.toString();} catch (SQLException e) {e.printStackTrace();}finally{con.close();}System.out.println("Page count ends....");return SUCCESS;}/**查詢對應頁面應有的結果集記錄*/public String showPage() throws SQLException{System.out.println("Show page begins...");int m = pageIndex * 4;//m-3 ~ mConnection con = new SQLConnection().getConnection();String sql;if( m == 4){sql="select* from desease where id<5";}else{sql="select top 4 * from desease where (id not in(select top "+(m-4)+" id from desease))";//"select * from desease where id<"+index+"";}try {Statement st = con.createStatement();ResultSet rs = st.executeQuery(sql);result = resultSetToJson(rs);} catch (SQLException e) {e.printStackTrace();}finally{con.close();}System.out.println("Show page ends....");return SUCCESS;}/**查詢的結果集記錄直接變成JSON對象*/public String resultSetToJson(ResultSet rs) throws SQLException,JSONException {// json數組 JSONArray array = new JSONArray();// 擷取列數 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍曆ResultSet中的每條資料 while (rs.next()) { JSONObject jsonObj = new JSONObject(); // 遍曆每一列 for (int i = 1; i <= columnCount; i++) { String columnName =metaData.getColumnLabel(i); String value=rs.getString(columnName);jsonObj.put(columnName, value); } array.element(jsonObj);} return array.toString(); } }
4. 結果如下:
Strust2 + JSON + jquery 分頁表格顯示資料