Shahzeb Khan
Shahzeb Khan

Reputation: 3642

Spring MVC : How to pass model object to controller method with out using form

I have a controller class where i am adding an object to model, On view i can access it, and now i want to send this object back to a new controller method, Is it possible to do it with out using form? and example code is:

Here i am adding 'details' to model.

@RequestMapping(...)
public ModelAndView method1() {
       .....
        mv.addObject("details", details);
         mv.setViewName(REVIEW_PAGE);
         return mv;
}

I have an "Ok" button on review page where details are reviewed. Now i want to send this "details" object back to a new method for submission. i want to access the details object in this second method of same controller class.

I have tried to add this as model attribute (as you can see in following code) but i am getting null values inside details object.

@RequestMapping(....)
public ModelAndView method2(@ModelAttribute("details") Details details){
//access details object here        

}

The flow is like : ( add details in model (method1) --> send to view for review --> confirm (click ok) --> send back for submission (method2))

I am new to Spring MVC so if there are mistakes in my question, i am sorry for that.

Upvotes: 0

Views: 3063

Answers (1)

Affe
Affe

Reputation: 47974

You can tell Spring to keep a copy of the model on the server side by using the @SessionAttributes annotation on the controller

@Controller
@SessionAttributes("details")
public class TheController {
}

This comes with some caveats. The default built-in implementation is pretty basic and does not, for example, account for multi-tab browsers using the same session across tabs. It also has no automatic cleanup. You have to manually call session.setComplete() when you are done with the data.

Upvotes: 2

Related Questions