Load objects before action, and share those objects in action and freemaker modules, possible?
Say a logged in user hits the url:
www.example.com/forum/234
Before the spring mvc action fires, I want to load the User Object, the user's permission, the Forum object.
Now I want to share these objects accross this request. So other classes can look to see, in the current request, for a User
, Permission
and Forum
object.
Potentially it would be cool if a custom freemarker module could also referen开发者_开发百科ce these objects if they are available.
is this possible?
First, consider using spring-security, whose filters do everything you need.
If you want to do all by hand, then you have at least two options: - use servlet filters - use spring handler interceptor (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor).
In both cases store this data in request attributes.
Another option is to create bean with request scope which will store your data.
As for Freemarker, you must provide own subclass of FreemarkerViewResolver, which will return subclass of FreeMarkerView in requiredViewClass() method. Add your objects in exposeHelpers() method in this FreeMarkerView subclass.
精彩评论