0

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.

1
  • why do you need to re-submit the model which is already available at server-sider? Commented Jan 25, 2013 at 6:44

1 Answer 1

2

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.

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

3 Comments

actually i am not using session
then you will need to do something to send the information back from the client side, a form is certainly simplest.
It's Either SessionAttributes or Form. In the form, you don't necessarily need inputs. just <c:out> are fine to display information on the page to review. the OK button is still a type submit. with your modelAttribute and action url is where you want to send your data.

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.