user1199476
user1199476

Reputation: 121

Spring MVC object passed with model attribute is empty

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

Upvotes: 2

Views: 3947

Answers (2)

Cooler
Cooler

Reputation: 319

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>

Upvotes: 0

bobharris
bobharris

Reputation: 173

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.

Upvotes: 1

Related Questions