Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access Spring MVC annotated session vars across multiple controllers?

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?

like image 944
Travelsized Avatar asked Oct 14 '10 14:10

Travelsized


People also ask

What is @SessionAttributes in Spring MVC?

@SessionAttribute annotation retrieve the existing attribute from the session. This annotation allows you to tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.


1 Answers

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()

like image 110
Daniel Alexiuc Avatar answered Sep 21 '22 01:09

Daniel Alexiuc