在打开任何jsp页面之前调用拦截器

时间:2014-02-03 10:24:21

标签: spring-mvc

我想在spring mvc application中打开任何jsp页面之前调用一个拦截器。

我的所有jsp页面都在webapps文件夹中。

任何人都可以向我提供详细信息吗?

1 个答案:

答案 0 :(得分:0)

我认为你的意思是“我想在任何请求中调用拦截器”,对吧? 假设这一点,在您的调度程序上下文中,您可以配置拦截器,例如:

<mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <mvc:exclude-mapping path="/login"/>
        <mvc:exclude-mapping path="/css/**"/>
        <mvc:exclude-mapping path="/js/**"/>
        <mvc:exclude-mapping path="/fonts/*.*"/>
        <mvc:exclude-mapping path="/img/**"/>
        <bean id="checkListInterceptor" class="your.package.Interceptor">
            <property name="userService" ref="webappUserService" />
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

你的拦截器实现应该是一些思考:

public class CheckListInterceptor implements HandlerInterceptor {


    private IWebappUserService userService;

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object arg2, Exception excp)
            throws Exception {
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
            Object arg2, ModelAndView mav) throws Exception {
        //do stuf..
        request.setAttribute("attrName", attrValue);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
            Object arg2) throws Exception {
        return true; //watch it: returning false prevents page to be served!
    }


    public void setUserService(IWebappUserService userService) {
        this.userService = userService;
    }

}