gpt4 book ai didi

java - HandlerInterceptorAdapter 不起作用

转载 作者:行者123 更新时间:2023-12-04 05:30:25 24 4
gpt4 key购买 nike

请帮助 Spring MVC 新手。我很难做 HandlerInterceptorAdapter工作。
这是代码和配置文件。
mvc-dispatcher-servlet.xml:

  <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.myproject" />

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="loginInterceptor" />
</list>
</property>
</bean>


<bean id="loginInterceptor" class="com.myproject.util.login.LoginInterceptor" />


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

登录拦截器.java:
    package com.myproject.util.login;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOG = Logger.getLogger(LoginInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LOG.debug("intercepted");
return super.preHandle(request, response, handler);
}
}

我试图让这个拦截器为任何 Controller 运行。也许, ControllerClassNameHandlerMapping , 不是用于此目的的最佳选择。请帮忙。

最佳答案

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/somepath/**" />
<bean class="com.mydomain.myapp.SomeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

我发现“mvc:interceptors”代替了 XML beans - 见: Spring MVC: Why does this <mvc:interceptors> declaration work, but not the traditional XML?

关于java - HandlerInterceptorAdapter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12699569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com