Announcement Announcement Module
Collapse
No announcement yet.
Pre-method (maybe aspect related) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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("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
    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.

    Comment

    Working...
    X