0

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..

2
  • so clarify for me, once branch is created you want to call another controller and pass it branch object? Commented Oct 9, 2014 at 4:37
  • 1
    hi,yea somethin' like that..after i pass it i want to compare it to the new branch object that has been created after i click the submit button. Commented Oct 9, 2014 at 6:23

2 Answers 2

1

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

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

Comments

1

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.

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.