为什么我的DispatcherServlet配置在Spring中不起作用?

时间:2016-06-30 14:32:08

标签: spring spring-mvc

我想确定他是否有权访问网址。但是我想给所有的css,js都可以访问。所以我配置web.xml就像这样:

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

    <mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="com.test.web.interceptor.MyInterceptor">
            <property name="loginUrl">
                <value>http://localhost:8080/my-web/login.jsp</value>                   
            </property> 
        </bean>
    </mvc:interceptor>
</mvc:interceptors> 

并设置spring-servlet.xml,如下所示:

  <mvc:resources location="/css/" mapping="/css/**" />
  <mvc:resources location="/js/" mapping="/js/**" />
像这样的

和MyInterceptor:

public class MyInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest arg0,
        HttpServletResponse arg1, Object arg2, Exception arg3)
        throws Exception {
    // TODO Auto-generated method stub

}

@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
        Object arg2, ModelAndView arg3) throws Exception {
    // TODO Auto-generated method stub

}

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    response.setCharacterEncoding("UTF-8");
    StringBuffer currentUrl = request.getRequestURL(); 
    String path = request.getServletPath(); 
     if(path.matches( "(.*/((css)|(js)|(images)).*)"
    )){
        System.out.println(path);
        return true;
    }
     //check token,if null return to login.jsp

  }

在此之后,我认为可以访问css和js请求而不是我的拦截器。这是错误的。现在我有两个问题:

  1. 我设置了mvc:resources,书中说它不会被DispatcherServlet处理,为什么css或js url请求仍然会转到MyInterceptor?
  2. 我访问了index.jsp页面,在MyInterceptor中我可以得到css请求,js请求,但为什么我没有得到jsp页面请求?

0 个答案:

没有答案