我想确定他是否有权访问网址。但是我想给所有的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请求而不是我的拦截器。这是错误的。现在我有两个问题:
mvc:resources
,书中说它不会被DispatcherServlet处理,为什么css或js url请求仍然会转到MyInterceptor?