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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.