Announcement Announcement Module
No announcement yet.
Pre-method (maybe aspect related) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pre-method (maybe aspect related)

    Hi all,

    I am writing a web application using annotated controllers (@Controller on the class and @RequestMapping("") on methods).

    But almost all methods I have put the very same data on model (login URL, logout URL, current user, etc) and I would like to do this setup in a cleaner way.

    Is it possible to use an annotation or something like that to always call a method passing the HttpServletRequest and a ModelMap to it as arguments?

    Thanks in advance.

  • #2
    Maybe a HandlerInterceptorAdapter

    I use a HandlerInterceptorAdapter for that. I have a class like this:

    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    public class ToolInjectorInterceptor extends HandlerInterceptorAdapter {
    	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    		if (modelAndView != null) {
    			modelAndView.addObject("stringTool", new StringTool());
    and configure its usage in my servlet context like this:

    <!-- default handler mappings to process @RequestMapping Annotations - needed as we customize -->
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
    		<property name="interceptors">
    				<ref bean="openSessionInViewInterceptor"/>
    				<bean class="ToolInjectorInterceptor" />
    That way I can enrich my model with whatever I need in all request processing and the interceptor also has access to the request.