[英]How to save dynamic data into my database with same name using Struts 2
我想知道如何使用Struts2將具有相同名稱的多個數據同時保存到數據庫中,我有多個項目,數量,金額,描述,單價...它們在動態表中。這是我的代碼下面
public String createPO() {
if (isAuthorizedUser(authorizedUsers)) {
dao = HibernateDAOFactory.getInstance();
order = new OrderEntity()
order.setQuantity(quantity);
order.setAmount(amount);
order.setDescription(description);
order.setUnit(unit);
order.setUnitPrice(unitPrice);
order.setUser(user);
System.out.println(order.getOrderid());
dao.getOrderDAO().isSaved(order);
return SUCCESS;
}
return logout();
}
這是我的JSP頁面。 單擊添加按鈕后,它將創建另一個具有相同名稱的字段。
<table id="orderTable" class="table table-bordered">
<tr>
<td style="background-color: #40926f; color: white;"><strong><center>Quantity</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Unit</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Description</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Unit Price</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Amount</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Add</center></strong></td>
<td style="background-color: #40926f; color: white;"><strong><center>Delete</center></strong></td>
</tr>
<tr id="orderDetailsTr" class="order">
<td>
<center>
<input required style="height: 35px; width: 100px;" type="text" placeholder="QTY"
name="quantity"
id="quantity" class="quantity" >
</center>
</td>
<td>
<center>
<input required style="height: 35px; width: 200px;" type="text" placeholder="Unit"
name="unit"
id="unit" >
</center>
</td>
<td>
<center>
<input required style="height: 35px; width: 300px;" type="text" placeholder="Description"
name="description"
id="description" >
</center>
</td>
<td>
<center>
<input required style="height: 35px; width: 100px;" type="text" placeholder="Unit Price"
name="unitPrice"
id="unitPrice" class="unitPrice" >
</center>
</td>
<td>
<center>
<input required style="height: 35px; width: 100px;" type="text" placeholder="Amount"
name="amount"
id="amount" class="amount" >
</center>
</td>
<td><input type="button" class="btn btn-primary" id="addmorePOIbutton" value="Add"/></td>
</tr>
</table>
要將數據從表轉換為列表,應使用一個屬性
List<OrderEntity> list = new ArrayList<OrderEntity>();
//getter and setter
public String createPO() {
if (isAuthorizedUser(authorizedUsers)) {
dao = HibernateDAOFactory.getInstance();
for (List<OrderEntity> order: list){
System.out.println(order.getOrderid());
dao.getOrderDAO().isSaved(order);
}
return SUCCESS;
}
return logout();
}
在JSP中,您應該修改輸入字段的名稱,以便它們包括對list屬性的綁定。
您可以在提交事件之前使用JavaScript修改名稱
for(var i=0; i < table.rows.length; i++) {
var quantity= table.rows[i].cells[0].getElementsByTagName("input");
quantity[0].name= "list["+i+"].quantity";
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.