2017-03-08 96 views
0
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    <style> 
     .col-lg-10{ 
      background-color: #ffffcc; 
      margin: 10px; 
     } 
    </style> 
    <script> 
     var array1=new Array()(); 
     document.getElementById("sales_Details").value=array1; 

     function add(){ 
      var reference=document.getElementById('sales_ref_No').value; 
      var product=document.getElementById('product_ID'); 
      var productID=product.options[product.selectedIndex].text; 
      var productPrice=document.getElementById('product_ID').value; 
      var description=document.getElementById('sales_Description').value; 
      var unit=document.getElementById('sales_Unit').value; 
      var total=Number(document.getElementById('sales_Total').value); 
      var price= parseFloat(productPrice*unit); 
      total+=price; 

      var table=document.getElementsByTagName('table')[0]; 

      var newRow=table.insertRow(1); 

      var cell1=newRow.insertCell(0); 
      var cell2=newRow.insertCell(1); 
      var cell3=newRow.insertCell(2); 
      var cell4=newRow.insertCell(3); 

      cell1.innerHTML=productID; 
      cell2.innerHTML=description; 
      cell3.innerHTML=unit; 
      cell4.innerHTML=price; 

      document.getElementById('sales_Total').value=total; 

      array1.push([reference,productID,description,unit,price]); 
     } 

    </script> 
</head> 
<body> 
    <jsp:include page="navigationBar.jsp"></jsp:include> 
    <div class="container"> 
     <%=login_Location_ID%> 
     <div class="col-lg-1"></div> 
     <div class="col-lg-10"> 
      <form class="form-horizontal" action="SalesController" method="POST"> 
       <fieldset> 
        <legend>Fill in the information</legend> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">Sales Reference No</label> 
         <div class="col-sm-8"> 
          <input type="text" class="form-control" id="sales_ref_No" name="sales_ref_No" required/> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">Create By</label> 
         <div class="col-sm-8"> 
          <input type="text" class="form-control" id="user_ID" name="user_ID" required/> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">Product</label> 
         <div class="col-sm-8"> 
          <select class="form-control" id="product_ID" name="product_ID"> 
           <c:forEach var="product" items="${product}"> 
            <option value="<c:out value="${product.product_Price}"/>"><c:out value="${product.product_ID}"/></option> 
           </c:forEach> 
          </select> 
         </div> 
        </div> 

        <div class="form-group"> 
         <label class="control-label col-sm-2">Description</label> 
         <div class="col-sm-8"> 
          <input type="text" class="form-control" id="sales_Description" name="sales_Description"/> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">Unit</label> 
         <div class="col-sm-4"> 
          <input type="number" class="form-control" id="sales_Unit" name="sales_Unit" required/> 
         </div> 
         <button type="button" onclick="add();">Add</button> 
        </div> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">Total</label> 
         <div class="col-sm-4"> 
          <input type="text" class="form-control" id="sales_Total" name="sales_Total" readonly/> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label class="control-label col-sm-2">location</label> 
         <div class="col-sm-4"> 
          <select class="form-control" name="location_ID"> 
           <c:forEach var="location" items="${location}"> 
            <option value="<c:out value="${location.location_ID}"/>"><c:out value="${location.location_ID}"/></option> 
           </c:forEach> 
          </select> 
         </div> 
        </div> 
        <% 
         if(request.getAttribute("insertError")!=null){ 
        %> 
        <p style="text-align: center; background-color: transparent;color:red"><c:out value="${insertError}"/> is Existed</p> 
        <% 
         } 
        %> 
        <table border="3"> 
         <tr> 
          <th>Product ID</th> 
          <th>Description</th> 
          <th>Unit</th> 
          <th>Price</th> 
         </tr> 
        </table> 
        <input type="hidden" id="sales_Details" name="sales_Details"/> 
        <input type="hidden" name="action" value="insertSales"/> 
        <div class="form-group">   
         <div class="col-sm-offset-2 col-sm-10"> 
          <button type="submit" class="btn btn-default">Submit</button> 
          <button type="reset" class="btn btn-default">Clear</button> 
         </div> 
        </div> 
       </fieldset> 
      </form> 
     </div> 
     <div class="col-lg-1"></div>    
    </div> 
    <div class="container"><jsp:include page="footer.jsp"></jsp:include></div> 
</body> 

將2D ArrayList傳遞給Servlet?

你好,我是在JSP中的新手,我想問的東西。

我有一個訂單表格,用戶可以添加多個項目並提交...訂單數據存儲在array1裏面,如圖所示。

我現在的問題是,我不知道應該使用什麼語法將ArrayList傳遞給servlet。

我試過使用input type=hidden name="array1" id="array1" value=array1",但它似乎並沒有工作。

+2

歡迎來到SO,請將您的代碼以textform形式發佈。 –

+0

可能會幫助您:http://stackoverflow.com/questions/26976639/sending-a-2d-array-jquery-post-to-java-servlet –

+0

如果您嘗試將複雜對象傳遞給服務器,請使用POST方法。 –

回答

0

您可以爲您的數據使用JSON格式。 Google: "json"

想法是將您的數據轉換爲JSON並將其作爲文本(字符串)傳遞,然後解析它(您可以使用GsonJackson)。

JSON例如: https://www.w3schools.com/js/js_json_arrays.asp

1-d陣列

[ "Ford", "BMW", "Fiat" ] 
號碼

2- d陣列

[ 
    ["2009-01", 324, 1075, 940, 441, 1040, 898, 1343], 
    ["2009-02", 295, 958, 904, 434, 1038, 793, 1246 ] 
] 

在這裏,你可以看到如何將數據從形式轉換成JSON。然後將字符串 dataFromForm傳遞給服務器並解析它。 Serialize form data to JSON