我想知道如何使用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>
答案 0 :(得分:0)
要将数据从表格转换为列表,您应该使用属性
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";
...
}