When in a method of a controller I need to retrieve some data from the session (often an ID that I use for user permissions and per user customization of the webpage), I write methods like this:
@RequestMapping("something")
public ModelAndView doSomething(HttpSession session) throws AuthorizationException{
PerUserModelBean bean = getSessionRelatedInfo(session);
bean.checkPermissions();
...do my stuff...
}
Which is boring because I have to repeat the session parameter for each method and I have to put that code at the beginning of each method. Not only boring, if I want to change that I have to go across all methods and change it. I’d like that session info retriavial and the checkPermissions method get called automatically before my method, and then somehow have the bean variable (perhaps as a threadlocal?) automatically filled according to the session contents. Is there some way to achieve this?
Place the below method to a super class which you can use as a Base for common functionality. I am sure you have one already in place so all child classes will inherit it.
For executing it before every method execution: you may use Filters/Interceptors/AOP, someone else may be able to give you more accurate ideas about that.