Good day. I got a form on the client side. And in that form i got a number of rows. Each row contains 3 fields (date, id and amount of money). Client can add rows (with the help of JavaScript) so server side doesn`t know how many rows will it be.
I need to map this form with the class, representing this data.
public class RowStruct
{
private Long id;
private Double amount;
private Date date;
}
public class FormStruct
{
private ArrayList<RowStruct> arrData;
{
So i need data from my form to be mapped with FormStruct object.
@RequestMapping("/send")
public ModelAndView getList(HttpServletRequest req,FormStruct formStruct)
{
ModelAndView model = new ModelAndView();
}
Is it possible and how do i do it?
you have to use an index on the clientside/html.
<input name="arrData[0].id" />
<input name="arrData[0].amount" />
<input name="arrData[0].date" />
Every time you add a row you have to increase the index. Unless you don't remove a row it will work like this.
I suppose you allow your clients to delete rows, so this unavoidably creates gaps between indexes. But you don't have to fix this on clientside, after the model has reached the controller you can filter your arrData for NULL references.
@RequestMapping("/send")
public ModelAndView getList(HttpServletRequest req,FormStruct formStruct)
{
Iterator<RowStruct> it = formStruct.getArrData().iterator();
while (it.hasNext()) {
if (it.next() == null)
it.remove();
}
ModelAndView model = new ModelAndView();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With