简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM