Nico
Nico

Reputation: 343

Spring passing object/string between controllers (GET & POST)

I've been struggling with passing some value between controller.

I have one controller like this:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addGet(HttpServletRequest request, @ModelAttribute(value="branch") Branch branch, Model model, blahblahblah)
//What I want to pass and re use:
String loadRespond;
try{
    loadRespond= *SOME LOAD STRING METHOD*;
    branch= branchManager.convertString(loadRespond); //METHOD TO SPLIT STRING & INDUCT TO OBJECT
}catch{exception){
    //blabla
}

After I successfully inducted all the attributes into the object branch,i show them all through a binding form. What i want to do is, when i'm going to update the data/change some attribute, i want to compare the old branch to the new changed branch. This means that i have to pass the old branch object or the loadRespond string onto the POST method so that can be used. Do anyone have any idea of how to do this? Maybe to assign it to hidden type field in the jsp? and then use it on the controller with request mapping /add of method type post? Thanks..I'm a newbie..

Upvotes: 0

Views: 3463

Answers (2)

Aeseir
Aeseir

Reputation: 8434

As San Krish notes in his answer the most common way is to use @SessionAttributes and pass objects/data using them.

This is useful if you don't worry about user moving backwards and forwards in a page, or want basic control of the object.

Now if you want to have a chain where controller 1 passes to controller 2 which may pass to controller 3 your best bet is to implement web flows.

Summary: For short and sweet and quick: SessionAttributes is the way to go, example here http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/

For chain passing, greater control and validation use Spring Web Flows.

Upvotes: 1

Santhosh
Santhosh

Reputation: 8207

Why don't you try out with session scope ?

store your old branch into the session . and when you get the new object compare with the old one (by retrieving from session)

You can save into session as any of both,

 request.getSession().setAttribute("sessionvar", "session value");
 @SessionAttributes("sessionvar")

A nice Example here to start with it.

Side-note : your question title doesnt quite expalain your problem and the solutions may vary

Upvotes: 1

Related Questions