1. 程式人生 > >表單多條資料提交

表單多條資料提交

1.第一種方法:表單提交,以欄位陣列接收

  • HTML程式碼如下:

<h1>submitUserList_1</h1>

<form action="${pageContext.request.contextPath }/customer/saveCustomer" method="post">

使用者名稱稱:<input type="text" name="custName"/><br><br>

年齡:<input type="text" name="custAge"/><br><br>

使用者名稱稱:<input type="text" name="

custName"/><br><br>

年齡:<input type="text" name="custAge"/><br><br>

<input type="submit" value="儲存"><br><br>

</form>

  • Java程式碼如下:

@RequestMapping(value="/customer/saveCustomer",method=RequestMethod.POST)

public String saveCustomer(

@Param("custName") String[] custName

,

@Param("custAge") Integer[] custAge) throws Exception{

for (int i = 0; i < custAge.length; i++) {

System.out.println(custAge[i]);

}

for (int i = 0; i < custName.length; i++) {

System.out.println(custName[i]);

}

return "redirect:/customer/listCustomer";

}

2.第二種方法:表單提交,以BeanListModel接收

這種方法有個問題就是

  • HTML程式碼如下:

<h1>submitUserList_2</h1>

<form action="${pageContext.request.contextPath }/customer/saveCustomer2" method="post">

使用者名稱稱:<input type="text" name="customerList[0].custName"/><br><br>

年齡:<input type="text" name="customerList[0].custAge"/><br><br>

使用者名稱稱:<input type="text" name="customerList[2].custName"/><br><br>

年齡:<input type="text" name="customerList[2].custAge"/><br><br>

<input type="submit" value="儲存"><br><br>

</form>

  • Java程式碼如下:

@RequestMapping(value="/customer/saveCustomer2",method=RequestMethod.POST)

public String saveCustomer2(@Param("customerList") CustomerModel customerModel) throws Exception{

System.out.println(customerModel.getCustomerList());

return "redirect:/customer/listCustomer";

}

public class CustomerModel {

private List<Customer> customerList ;

public List<Customer> getCustomerList() {

return customerList;

}

public void setCustomerList(List<Customer> customerList) {

this.customerList = customerList;

}

}