Results 1 to 2 of 2

Thread: Pre-method (maybe aspect related)

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Default Pre-method (maybe aspect related)

    Hi all,

    I am writing a web application using annotated controllers (@Controller on the class and @RequestMapping("myMapping.do") 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. #2
    Join Date
    Jul 2006
    Posts
    26

    Default Maybe a HandlerInterceptorAdapter

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

    Code:
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class ToolInjectorInterceptor extends HandlerInterceptorAdapter {
    
    	@Override
    	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:

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •