多選提交 + java後臺獲取解析信息

html

<form action="back/resourceList" method="post">
     <ul class="seachform" >
            <li><label>級別:</label>
               <div class="vocation">
                <select class="select3" name="grade" id="grade" >
                <c:choose> 
                    <c:when test="${search.grade eq 1}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">&nbsp;初&nbsp;中&nbsp;</option> 
                         <option value="1" selected="selected">&nbsp;高&nbsp;中&nbsp;</option>
                    </c:when>
                    <c:when test="${search.grade eq 0}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0" selected="selected">&nbsp;初&nbsp;中&nbsp;</option> 
                         <option value="1">&nbsp;高&nbsp;中&nbsp;</option>
                    </c:when> 
                     <c:otherwise>
                         <option value="-1" selected="selected">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">&nbsp;初&nbsp;中&nbsp;</option> 
                         <option value="1">&nbsp;高&nbsp;中&nbsp;</option>
                    </c:otherwise>
             </c:choose>
             </select>
            <input id="uid" name="uid" type="text"  hidden="hidden" value="${uid}"/>
        </li>
         <li><label>類型:</label>
            <div class="vocation">
                 <select class="select3" name="subclass" id="subclass" > 
                 <c:choose>
                    <c:when test="${search.subclass eq 'word'}">
                         <option value="all">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="word" selected="selected">word</option> 
                         <option value="pdf">pdf</option> 
                         <option value="ppt">ppt</option> 
                         <option value="picture">&nbsp;圖&nbsp;片&nbsp;</option> 
                    </c:when>
                    <c:when test="${search.subclass eq 'pdf'}">
                         <option value="all">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="word" selected="selected">word</option> 
                         <option value="pdf">pdf</option> 
                         <option value="ppt">ppt</option> 
                         <option value="picture">&nbsp;圖&nbsp;片&nbsp;</option> 
                    </c:when>
                    <c:when test="${search.subclass eq 'ppt'}">
                         <option value="all">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="word">word</option> 
                         <option value="pdf">pdf</option> 
                         <option value="ppt" selected="selected">ppt</option> 
                         <option value="picture">&nbsp;圖&nbsp;片&nbsp;</option> 
                    </c:when>
                    <c:when test="${search.subclass eq 'picture'}">
                         <option value="all">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="word">word</option> 
                         <option value="pdf">pdf</option> 
                         <option value="ppt">ppt</option> 
                         <option value="picture" selected="selected">&nbsp;圖&nbsp;片&nbsp;</option> 
                    </c:when>
                    <c:otherwise>
                         <option value="all" selected="selected">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="word">word</option> 
                         <option value="pdf">pdf</option> 
                         <option value="ppt">ppt</option> 
                         <option value="picture">&nbsp;圖&nbsp;片&nbsp;</option> 
                    </c:otherwise>
                 </c:choose>
                 </select>
            </div>
        </li>
         <li><label>狀態:</label>
            <div class="vocation">
                 <select class="select3" name="state" id="state" > 
                 <c:choose>
                    <c:when test="${search.state eq 0}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0" selected="selected">未審覈</option> 
                         <option value="1">已通過</option> 
                         <option value="2">未通過</option> 
                         <option value="3">已下架</option> 
                    </c:when>
                    <c:when test="${search.state eq 1}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">未審覈</option> 
                         <option value="1" selected="selected">已通過</option> 
                         <option value="2">未通過</option> 
                         <option value="3">已下架</option> 
                    </c:when>
                    <c:when test="${search.state eq 2}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">未審覈</option> 
                         <option value="1">已通過</option> 
                         <option value="2" selected="selected">未通過</option> 
                         <option value="3">已下架</option> 
                    </c:when>
                    <c:when test="${search.state eq 3}">
                         <option value="-1">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">未審覈</option> 
                         <option value="1">已通過</option> 
                         <option value="2">未通過</option> 
                         <option value="3" selected="selected">已下架</option> 
                    </c:when>
                    <c:otherwise>
                         <option value="-1" selected="selected">&nbsp;全&nbsp;部&nbsp;</option> 
                         <option value="0">未審覈</option> 
                         <option value="1">已通過</option> 
                         <option value="2">未通過</option> 
                         <option value="3">已下架</option> 
                    </c:otherwise>
                 </c:choose>
                 </select>
            </div>
         </li>
        <li><label>資源名稱</label><input id="searchName" name="searchName" type="text" class="scinput"  style="width: 520px;"/></li>
        <li><label>&nbsp;</label><input name="" type="submit" class="scbtn" value="搜索" /></li>
        <li><label>&nbsp;</label><input name="" type="button" class="scbtn" value="一鍵審覈" onclick="sendDataTo('test')"/></li>
     </ul>
    </form>

  <table class="tablelist" id="test">
    <thead>
    <tr>
    <th><input name="chooseAll" name="chooseAll" type="checkbox" onclick="swapCheck()"/></th>
      <th>編號</th>
      <th>資源名稱</th>
      <th>上傳者</th>
      <th>資源級別</th>
      <th>點擊量</th>
      <th>上傳時間</th>
      <th>資源類型</th>
      <th>審覈狀態</th>
      <th>更多操作</th>

      </tr>
      </thead>
      <tbody>
      <c:forEach items="${page.list}" var="resource" varStatus="vs1">
       <tr>
       <td align="center"><input name="resource_id" id="resource_id" type="checkbox" value="${resource.id}" /></td>
       <td  align="center">${(vs1.index+1)+((pageNow-1)*pageSize)+(page.currentPage-1)*15}</td><!--編號自增  -->
       <td  align="center">${resource.title}</td>
       <td  align="center">${resource.writer}</td>
       <c:if test="${resource.grade eq 0 }">
        <td  align="center">初中</td>
       </c:if>
       <c:if test="${resource.grade eq 1 }">
        <td  align="center">高中</td>
       </c:if>
       <td  align="center">${resource.clicks}</td>
       <td  align="center">${resource.time}</td>
       <td  align="center">${resource.subclass}</td>
       <c:if test="${resource.state eq 0 }">
        <td  align="center">未審覈</td>
       </c:if>
       <c:if test="${resource.state eq 1 }">
        <td  align="center">通過</td>
       </c:if>
        <c:if test="${resource.state eq 2 }">
        <td  align="center">未通過</td>
       </c:if>
       <c:if test="${resource.state eq 3 }">
        <td  align="center">已下架</td>
       </c:if>
       <td  align="center"><a href="back/show_modify_resource?id=${resource.id}"
                  class="tablelink">編輯</a>     
                  <a href="back/resourceView?url=${resource.url}"
                  class="tablelink">預覽</a>
                  <a href="back/downloadfile?url=${resource.url}"
                  class="tablelink">下載</a>   
                <a href="javascript:void(0)" onclick="javascript:deleteresource(${resource.id});"
        class="tablelink"> 刪除</a></td>
       </tr>
       </c:forEach>
      </tbody>
  </table>

javascript

<!--實現全選/取消全選 + 全部提交到後臺-->
   <script type="text/javascript">  
       //checkbox 全選/取消全選  
       var isCheckAll = false;  
       function swapCheck(){  
           if (isCheckAll) {  
               $("input[type='checkbox']").each(function() {  
                   this.checked = false;  
               });  
               isCheckAll = false;  
           } else {  
               $("input[type='checkbox']").each(function() {  
                   this.checked = true;  
               });  
               isCheckAll = true;  
           }  
       }  

       //通過dom判斷該列的checkbox是否被選中,並獲取該選中行的數據
       function sendDataTo(id){
           //獲取該table中所有的input元素,是一個數組
         var inputs = document.getElementById(id).getElementsByTagName("input");
         var allData = new Array();
         var value=[];
         var num = 0;
         for(var i = 0; i < inputs.length; i++){
             // 判斷該input中是否爲checkbox
             if(inputs[i].type == "checkbox" && inputs[i].checked){ //判斷該checkbox是否被選中,如果被選中獲取table行中的指定元素
                  //取值
                     value[value.length]=inputs[i].value;
                     num++;
             }
         };

  //       alert("共有"+num+"個複選框被選中,值爲"+value);

         var   url="back/checkAllResource"; //跳轉到tb_backResourceController 一鍵審覈
         var   data={
              allData : JSON.stringify(value),
            };
         $.post(url,data,function(data){
             if(data=="0"){
              alert("額,出錯了~");
             }
             else if(data=="1"){
              alert("恭喜,操作成功!");
              location.reload();//刷新本頁面
             }
         },"text" ); 
     }

   </script>    

JAVA後臺

/**
 * @author:pcc
 * @comments(方法的說明):實現一鍵審覈
 * @time 2018年5月31日 下午11:02:11
 * @param ()
 * @return ()
 * @returnType:void
 */
@RequestMapping("/checkAllResource")
    public void checkAllResource(HttpServletRequest request,HttpServletResponse response){  
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String allData = request.getParameter("allData");
        JSONArray jsonArray = new JSONArray(allData);
//      System.out.println(allData + "   " + jsonArray.length());
        //字符處理,去掉" " 和 [ ]
/*      allData = allData.replace("\"", "");
        allData = allData.replace("[", "");
        allData = allData.replace("]", "");
        allData = allData.replaceAll("on", "");*/
        //用正則表達式一行解決
        allData = allData.replaceAll("[\"\\[\\]]", "");//用""替換" [ ]
        String word[] = allData.split(","); //以 , 爲分隔成單詞,再轉成int的id
        int count=0;
        Pattern pattern = Pattern.compile("[0-9]*");   //正則,匹配數字
        for(int i=0;i<jsonArray.length();i++){ 
            Matcher matcher = pattern.matcher(word[i]);  
            if(matcher.matches()){ //判斷字符串是否可以轉爲數字
                int id = Integer.parseInt(word[i]); 
                int n = 0;
                n = tb_resourceService.updateResourceState(id);//通過id更新資源狀態
                if(n==0){
                    out.print("0");
                }
            }
            count++;
        }
        if(count == jsonArray.length()){
            out.print("1");
        }else{
            out.print("0");
        }
        out.flush();
        out.close();
 }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章