Is it possible to access Spring annotated MVC session sessions through multiple controllers?

I have a web application with Spring 3.0 and using Spring -MVC. I have several controllers configured 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 annotated variables to ModelMap with something like

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

This works great to save a variable in the current controller; I can access var from modelMap from any other method in this controller. But when the user clicks the page on another controller, although the same variable is specified in the @SessionAttributes attributes, it is not available in the second controller.

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

+4
source share
2 answers

No, this is not possible - SessionAttributes are poorly named in my opinion.

If you want to share these attributes between different controllers, you can explicitly put them in a session using:

session.setAttribute ()

+5
source

You can have a parent BaseController class that should not be the @Controller class and use the @SessionAttibutes({"clientLogin", "selectTab", "user", "redirectUrl"}) variable @SessionAttibutes({"clientLogin", "selectTab", "user", "redirectUrl"}) there. Remember that this class must fall into the MVC scan package. Then, when you need to use this in your real controllers, use as shown below.

 public String getAllDetails(@ModelAttributes("clientLogin") Client client){ client.getName(); return "somejsp"; } 
0
source

All Articles