将2D ArrayList传递给Servlet?

时间:2017-03-08 10:32:16

标签: java jsp servlets arraylist

<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>

您好,我是JSP的新手,我想问一些问题。

我在表单中有一个订单表,用户可以添加多个项目并提交...订单数据存储在图片中显示的array1内。

我现在的问题是,我不知道应该使用什么语法将ArrayList传递给servlet。

我尝试使用input type=hidden name="array1" id="array1" value=array1",但似乎无效。

1 个答案:

答案 0 :(得分:0)

您可以将JSON格式用于您的数据。 Google: "json"

我们的想法是将您的数据转换为JSON并将其作为文本(String)传递,然后解析它(您可以使用GsonJackson)。

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

1-D阵列

Notepad++

二维数组

[ "Ford", "BMW", "Fiat" ]

在这里,您可以看到如何将表单中的数据转换为JSON。然后传递String [ ["2009-01", 324, 1075, 940, 441, 1040, 898, 1343], ["2009-02", 295, 958, 904, 434, 1038, 793, 1246 ] ] 到服务器并解析它。 Serialize form data to JSON