简体   繁体   中英

How to save dynamic data into my database with same name using Struts 2

I would like to know how to save multiple data at the same time with same names into my database using Struts2 I have multiple items,quantity,amount,description,unit price...and they are inside in dynamic table..Here's my code below

 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();
}

And this is my JSP page. After I click add button it will create another fields with same names.

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

To convert data from a table to a list you should use a property

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();
}

In JSP you should modify names of input fields, so they include binding to the list property.

You can use javascript to modify names before submit event

for(var i=0; i < table.rows.length; i++) {  
    var quantity= table.rows[i].cells[0].getElementsByTagName("input");
    quantity[0].name= "list["+i+"].quantity"; 
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM