views:

23

answers:

1

I have a web app running Spring 3.0 and using Spring-MVC. I have a few controllers set up like this:

@Controller
@RequestMapping("/admin")
@SessionAttributes({"clientLogin", "selectTab", "user", "redirectUrl"})
public class AdminController {
...
}

@Controller
@SessionAttributes({"clientLogin", "selectTab", "user", "redirectUrl"})
public class PublicController {
....
}

I can add the annotated variables into the ModelMap with something like

map.addAttribute("user", "Bob");

That works fine to persist the variable in the current controller; I can access the var from the modelMap from any other method in that controller. But when the user hits a page in another Controller, even though the same variable is listed in the @SessionAttributes, it's not available in the second controller.

Is it possible to access these annotated variables across multiple controllers using the annotations?

+1  A: 

No it isn't possible - SessionAttributes are badly named in my opinion.

If you want to share these attributes across different controllers, you can explicitly put them into the session using:

session.setAttribute()

Daniel Alexiuc
Here's a link to a related discussion I had that you might find interesting: http://forum.springsource.org/showthread.php?t=77305
Daniel Alexiuc
That is interesting, thanks. Given the trouble you've had (though it seems to have changed from Spring 2.5 to 3.0) do you even attempt to use SessionAttributes anymore?
Travelsized
They are good for the purpose they have been designed for - simple request-response interactions. If you find you are using lots of conversational style flows with multiple controllers, then Spring MVC isn't really designed for this - you should look into Spring Webflow.
Daniel Alexiuc

related questions