2

i have problem with @ModelAttribute,

CustEntity have objects like "name" etc. also have list of BankAccEntity called bankAcc which has number and name.

in GET method when i use getBankAcc() my cust has arraylist with bankaccounts, but when i pass object "customer" from GET to POST, he has [] in BankAcc list;/

my code fragment is below:

    @RequestMapping(value = "/aaa.html", method = RequestMethod.GET)
public String aaaGet(Model m, Principal principal) {

...
    CustEntity cust = custService.getCustByUserName(principal);
    cust.getBankAcc();

    m.addAttribute("customer", cust);

...
}


@RequestMapping(value = "/aaa.html", method = RequestMethod.POST)
public String aaaPost(
        @ModelAttribute("customer") CustomerEntity cust,
        BindingResult results, RedirectAttributes redirectAttributes,
        Model m) {

    cust.getBankAcc();

    ...
}

regards, swierzy

skaffman
  • 398,947
  • 96
  • 818
  • 769
user1199476
  • 121
  • 2
  • 5
  • 11

2 Answers2

1

In aaaPost, CustomerEntity cust will be binding with the data in your form. That is, cust in aaaPost is not the one that you put in the model in aaaGet.

bobharris
  • 173
  • 3
  • 10
0

I also stuck with this problem and got a solution:

Add spring form to your page:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 

And use form like this:

<form:form action="/someUrl" modelAttribute="objectName" method="post">
     <form:input path="fieldName"/>
     <button type="submit">buttonName</button>
</form:form>
Cooler
  • 309
  • 3
  • 6
  • 17