1

I'm running into a situation where my model attribute is losing values between pages.

I've got two controller methods that handle GET and POST requests respectively.

GET Method

@RequestMapping(value = "/checkout/billing", method = RequestMethod.GET)
    public String getBillingPage(Model model, final HttpServletRequest request) throws CMSItemNotFoundException {
   // other code

   checkoutForm.setCustomFieldsForm(customFieldsForm);
   model.addAttribute("checkoutForm", checkoutForm);

   // other code
}

Debug View After GET Method completes enter image description here

POST Method

@RequestMapping(value = "/checkout/billing", method = RequestMethod.POST)
    public String submitPayment(
            @Valid @ModelAttribute("checkoutForm") final CheckoutForm checkoutForm,
            final BindingResult bindingResult,
            Model model,
            final HttpServletRequest request,
            final HttpServletResponse response) throws CMSItemNotFoundException 
    {}

Debug View When POST Method is Invoked enter image description here

The 1234 comes from the user entering that data in the form field. The other values should still be there and not null though.

What could be happening here?

0

1 Answer 1

2

Your model is not stored in the session. Each request creates a new model object. That's why it's empty.

You can add your model as a session attribute. Please find the documentation here https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

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

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.