1. 程式人生 > >jquery ajax配合SpringMVC實現區域性重新整理DIV

jquery ajax配合SpringMVC實現區域性重新整理DIV

  1. <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8.     String imgPath = request.getParameter("imgPath") == null ? ""
  9.             : request.getParameter("imgPath").toString().trim();  
  10.     System.out.println(imgPath);  
  11. %>  
  12. <!DOCTYPE HTML>  
  13. <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->  
  14. <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->  
  15. <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->  
  16. <!--[if gt IE 8]><!-->  
  17. <html class="no-js">  
  18. <!--<![endif]-->  
  19. <head>  
  20. <base href="<%=basePath%>"
    >  
  21. <title>反饋資訊</title>  
  22. <meta http-equiv="pragma" content="no-cache">  
  23. <meta http-equiv="cache-control" content="no-cache">  
  24. <meta http-equiv="expires" content="0">  
  25. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  26. <meta http-equiv="description" content="This is my page">  
  27. <meta name="viewport" content="width=device-width, initial-scale=1">  
  28. <link rel="stylesheet" type="text/css"
  29.     href="<%=basePath%>bootstrap-3.2.0-dist/css/bootstrap.css"></link>  
  30. <link rel="stylesheet"
  31.     href="<%=basePath%>bootstrap-3.2.0-dist/css/bootstrap-theme.css"
  32.     type="text/css"></link>  
  33. <script type="text/javascript" src="<%=basePath%>bootmetro/js/jquery-1.10.0.min.js"></script>  
  34. <script src="<%=basePath%>bootmetro/js/modernizr-2.6.2.min.js"></script>  
  35. <script type="text/javascript"
  36.     src="<%=basePath%>js/jquery-1.11.1.min.js"></script>  
  37. <script src="<%=basePath%>bootstrap-3.2.0-dist/js/bootstrap.min.js"></script>  
  38. <style type="text/css">  
  39. .footer {  
  40.     position: absolute;  
  41.     bottom: 0;  
  42.     width: 100%;  
  43.     /*margin-left:-40px;*/
  44.     /* Set the fixed height of the footer here */
  45.     height: 60px;  
  46.     background-color: #2F2F2F;/*#f5f5f5;*/
  47. }  
  48. body{  
  49.     /*padding:50px;*/
  50. }  
  51. .footer>.container {  
  52.     padding-right: 15px;  
  53.     padding-left: 15px;  
  54. }  
  55. </style>  
  56. </head>  
  57. <body>  
  58.     <!-- Fixed navbar -->  
  59.     <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">  
  60.       <div class="container" style="line-height:50px;">  
  61.       <div style="text-align: center;">  
  62.         <a style="color:white;font-size: 18px;text-decoration: none">反饋資訊</a>  
  63.       </div>  
  64.       </div>  
  65.     </nav>  
  66.      <div class="container" style="margin-top:20px;padding:50px">  
  67.         <div class="row-fluid">  
  68.             <div id="flushDIV"class="col-xs-12 col-sm-12 col-md-12" style="box-shadow:0 0 10px #666666;border-radius:8px;">  
  69.                 <table class="table table-hover" style="text-align: center;table-layout: fixed;">  
  70.                     <thead>  
  71.                     <tr>  
  72.                         <th style="text-align: center;">郵箱地址</th>  
  73.                         <th style="text-align: center;">裝置ID</th>  
  74.                         <th style="text-align: center;">電話號碼</th>  
  75.                         <th style="width:200px; text-align: center;">正文</th>  
  76.                         <th style="text-align: center;">時間</th>  
  77.                     </tr>  
  78.                     </thead>  
  79.                 <c:forEach items="${page.datas}" var="utcFeedback" varStatus="vs">  
  80.                     <tr id="${utcFeedback.deviceId }">  
  81.                         <td>${utcFeedback.email }</td>  
  82.                         <td>${utcFeedback.deviceId }</td>  
  83.                         <td>${utcFeedback.phoneNo }</td>  
  84.                         <!--  
  85.                         <td><a data-toggle="modal" data-target=".bs-example-modal-sm" href="javascript:void(0)">${utcFeedback.content }</a></td>  
  86.                         -->  
  87.                         <td>${utcFeedback.content }</td>  
  88.                         <td>${utcFeedback.opTimer }</td>  
  89.                     </tr>   
  90.                 </c:forEach>  
  91.                         <tr>  
  92.                             <td colspan="5">  
  93.                                 <c:if test="${page.totalCount > 0}">  
  94.                                     <c:choose>  
  95.                                         <c:when test="${page.pageNo == 1}">  
  96.                                             <a style="text-decoration: none">首頁</a>      
  97.                                             <a style="text-decoration: none">上一頁</a>      
  98.                                         </c:when>  
  99.                                         <c:otherwise>  
  100.                                             <a href="javascript:doPage(1);">首頁</a>      
  101.                                             <a href="javascript:doPage(${page.upPage});">上一頁</a>      
  102.                                         </c:otherwise>  
  103.                                     </c:choose>  
  104.                                     <c:choose>  
  105.                                         <c:when test="${page.pageNo == page.totalPage || page.totalPage == 0}">  
  106.                                             <a style="text-decoration: none">下一頁</a>      
  107.                                             <a style="text-decoration: none">末頁</a>      
  108.                                         </c:when>  
  109.                                         <c:otherwise>  
  110.                                             <a href="javascript:doPage(${page.nextPage});">下一頁</a>      
  111.                                             <a href="javascript:doPage(${page.totalPage});">末頁</a>      
  112.                                         </c:otherwise>  
  113.                                     </c:choose>  
  114.                                 <a style="text-decoration: none">當前第${page.pageNo}頁</a>      
  115.                                 <a style="text-decoration: none">共${page.totalPage}頁</a>      
  116.                                 </c:if>  
  117.                                 <a style="text-decoration: none">共${page.totalCount}條資料</a>      
  118.                             </td>  
  119.                         </tr>  
  120.                 </table>  
  121.             </div>  
  122.         </div>  
  123.      </div>  
  124.     <div class="footer">  
  125.         <div class="container">  
  126.         </div>  
  127.     </div>  
  128.     <script type="text/javascript">  
  129.         function doPage(n){  
  130.             $('#flushDIV').html("");  
  131.             $.ajax({  
  132.                    type:"POST",  
  133.                    url:"<%=basePath%>apps/queryFeedBackInfo.do",  
  134.                    data:{pageNo:n},  
  135.                    dataType: "html",   //返回值型別       使用json的話也可以,但是需要在JS中編寫迭代的html程式碼,如果格式樣式
  136.                    cache:false,  
  137.                    success:function(data){  
  138.                        //var json = eval('('+msg+')');//拼接的json串
  139.                        $('#flushDIV').html(data);  
  140.                    },  
  141.                    error:function(error){alert(error);}  
  142.                });  
  143.         }  
  144.     </script>  
  145. </body>  
  146. </html>  
conroller(具體DAO和Page類省略):
  1. /** 
  2.      * 顯示不同頁碼的反饋資訊 
  3.      * @param request 
  4.      * @param response 
  5.      * @return 
  6.      * @throws Exception 
  7.      */
  8.     @RequestMapping(value="/queryFeedBackInfo")  
  9.     public ModelAndView queryFeedBackInfo(HttpServletRequest request,HttpServletResponse response)throws Exception{  
  10.         UtcFeedbackDao utcFeedbackDao = new UtcFeedbackDao();  
  11.         int currentPage = request.getParameter("pageNo")==null?1:request.getParameter("pageNo").toString().trim()==""?1:Integer.parseInt(request.getParameter("pageNo").toString().trim());  
  12.         Pagination<UtcFeedback> page = null;//分頁幫助類
  13.         page = utcFeedbackDao.findByCondition(currentPage,pagesize);  
  14.         HashMap map = new HashMap();  
  15.         map.put("page", page);  
  16.         returnnew ModelAndView("app/temp",map);  
  17.     }  

temp.jsp:
  1. <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11. <head>  
  12. <base href="<%=basePath%>">  
  13. <title>My JSP 'temp.jsp' starting page</title>  
  14. <meta http-equiv="pragma" content="no-cache">  
  15. <meta http-equiv="cache-control" content="no-cache">  
  16. <meta http-equiv="expires" content="0">  
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18. <meta http-equiv="description" content="This is my page">  
  19. <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22. </head>  
  23. <body>  
  24.     <table class="table table-hover" style="text-align: center;table-layout: fixed;">  
  25.         <thead>  
  26.             <tr>  
  27.                 <th style="text-align: center;">郵箱地址</th>  
  28.                 <th style="text-align: center;">裝置ID</th>  
  29.                 <th style="text-align: center;">電話號碼</th>  
  30.                 <th style="width:200px; text-align: center;">正文</th>  
  31.                 <th style="text-align: center;">時間</th>  
  32.             </tr>  
  33.         </thead>  
  34.         <c:forEach items="${page.datas}" var="utcFeedback" varStatus="vs">  
  35.             <tr id="${utcFeedback.deviceId }">  
  36.                 <td>${utcFeedback.email }</td>  
  37.                 <td>${utcFeedback.deviceId }</td>  
  38.                 <td>${utcFeedback.phoneNo }</td>  
  39.                 <td>${utcFeedback.content }</td>  
  40.                 <td>${utcFeedback.opTimer }</td>  
  41.             </tr>  
  42.         </c:forEach>  
  43.         <tr>  
  44.             <td colspan="5"><c:if test="${page.totalCount > 0}">  
  45.                     <c:choose>  
  46.                         <c:when test="${page.pageNo == 1}">  
  47.                             <a style="text-decoration: none">首頁</a>      
  48.                             <a style="text-decoration: none">上一頁</a>      
  49.                         </c:when>  
  50.                         <c:otherwise>  
  51.                             <a href="javascript:doPage(1);">首頁</a>      
  52.                             <a href="javascript:doPage(${page.upPage});">上一頁</a>      
  53.                         </c:otherwise>  
  54.                     </c:choose>  
  55.                     <c:choose>  
  56.                         <c:when  
  57.                             test="${page.pageNo == page.totalPage || page.totalPage == 0}">  
  58.                             <a style="text-decoration: none">下一頁</a>      
  59.                             <a style="text-decoration: none">末頁</a>      
  60.                         </c:when>  
  61.                         <c:otherwise>  
  62.                             <a href="javascript:doPage(${page.nextPage});">下一頁</a>      
  63.                             <a href="javascript:doPage(${page.totalPage});">末頁</a>      
  64.                         </c:otherwise>  
  65.                     </c:choose>  
  66.                     <a style="text-decoration: none">當前第${page.pageNo}頁</a>      
  67.                     <a style="text-decoration: none">共${page.totalPage}頁</a>      
  68.                     </c:if> <a style="text-decoration: none">共${page.totalCount}條資料</a>      
  69.             </td>  
  70.         </tr>  
  71.     </table>  
  72. </body>  
  73. </html>