在Spring中正确配置mvc:interceptor
我有点问题。我需要在此拦截器中调用每个请求的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());
}
}
所以我将这一行放入servlet配置中,一切正常。
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="menuInterceptor" /><bean id="menuInterceptor" class="cz.cosi.DiMenuInterceptor" />
但是现在我必须更改配置和使用 <mvc:interceptors>
通过这种配置,我在postHandle方法中的modelAndView上得到了一系列空指针异常,因为每个请求多次调用postHandle方法。
<mvc:interceptors> <bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptors>
使用此配置,它可以工作,但仅适用于请求serverAdress /任何东西。对于请求serverAdress /任何东西/东西不调用postHandle。
<mvc:interceptors> <mvc:interceptor>
<mvc:mapping path="/*" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
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 mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:interceptors>
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptors>
<tx:jta-transaction-manager />
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
看来,问题可能与资源有关,因为没有正确加载图像,样式和javascript例外。没有mvc:resources可以正常工作,但这不是解决方案。
我的问题是,如何正确配置DiMenuInterceptor <mvc:interceptors>
。非常感谢你的建议。
stack:
2019-04-14 09:56:02,487 [http-8080-3] DEBUG (FilterChainProxy.java:195) ? Converted URL to lowercase, from: '/images/core/users/super_admin.png'; to: '/images/core/users/super_admin.png'2019-04-14 09:56:02,533 [http-8080-3] DEBUG (FilterChainProxy.java:202) ? Candidate is: '/images/core/users/super_admin.png'; pattern is /images/**; matched=true
2019-04-14 09:56:02,533 [http-8080-3] DEBUG (FilterChainProxy.java:158) ? /images/core/users/super_admin.png has an empty filter list
14.4.2011 9:56:02 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet spring threw exception
java.lang.NullPointerException
at cz.cosi.DiMenuInterceptor.postHandle(DiMenuInterceptor.java:41)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:163)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:595)
也许我找到了解决方案,但这绝对不是最好的。目前看来,这是可行的。
<mvc:interceptors> <mvc:interceptor>
<mvc:mapping path="/*" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/search/**" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/context/**" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/member/**" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
回答:
你需要指定你的路径包括子路径:/**"
不是/*
。
<mvc:interceptors> <mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
以上是 在Spring中正确配置mvc:interceptor 的全部内容, 来源链接: utcz.com/qa/415563.html