小编rin*_*ler的帖子

适当的mvc:Spring中的拦截器配置

我有点问题.我需要调用此拦截器中的每个请求postHandle方法:

public class DiMenuInterceptor extends HandlerInterceptorAdapter {

   @Autowired
   private IDiCategoryService categoryService;


   @Override
   public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

       modelAndView.addObject("category", categoryService.getCategoryInTree());
   }
}
Run Code Online (Sandbox Code Playgroud)

所以我把servlet配置到这行,一切正常.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="menuInterceptor" />

<bean id="menuInterceptor" class="cz.cosi.DiMenuInterceptor" />
Run Code Online (Sandbox Code Playgroud)

但现在我必须改变配置和使用 <mvc:interceptors>

使用这种配置,我在postHandle方法中的modelAndView上获得了一系列空指针异常,因为每个请求多次调用postHandle方法.

 <mvc:interceptors>
    <bean class="cz.cosi.DiMenuInterceptor" />
 </mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)

使用此配置,它正在工作,但仅适用于请求serverAdress /任何内容.对于请求serverAdress/anything/something是postHandle not called.

<mvc:interceptors>
   <mvc:interceptor>
     <mvc:mapping path="/*" />
     <bean class="cz.cosi.DiMenuInterceptor" />
   </mvc:interceptor>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)

servlet配置的一部分

<mvc:annotation-driven />

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />       

</bean>

<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources …
Run Code Online (Sandbox Code Playgroud)

spring interceptor

14
推荐指数
1
解决办法
5万
查看次数

标签 统计

interceptor ×1

spring ×1