springmvc拦截配置和静态资源访问问题

2018-12-05  本文已影响0人  匿名的我呀

---

# 访问过程

- 1.浏览器url --> tomcat 容器

- 2.被springmvc,拦截请求,匹配url

- 3.是否匹配,匹配就分发给Controller

- 4.如果匹配则Controller处理逻辑,返回视图,视图加上前后缀生成url,再次访问tomcat容器

- 5.如果是静态资源直接访问

---

# url-pattern

拦截器拦截静态资源,需要用mvc:resource声明,就不会被拦截

## '*.xxx'

  以指定后缀结尾的请求都交由DispatcherServlet处理

## '/'

将会覆盖容器的default servlet, 凡是在web.xml文件中找不到匹配的URL,它们的访问请求都将交给该Servlet处理(静态资源也将会拦截). 所以web.xml没有配置其他特殊路径的servlet, 基本上所有的请求都交由DispatcherServlet处理

## '/*'

错误的配置,会拦截*.jsp, *.jspx的请求, 使用这种配置最终要转发到一个JSP页面,仍然会由DispatcherServlet, 解析jsp地址, 不能根据jsp页面找到handler, 会报错

## 为什么tomcat下的WEB-INF目录的jsp文件无法直接访问

在%TOMCAT_HOME%/conf/web.xml中继承过来的JspServlet会处理该请求.

```

<servlet-mapping>

        <servlet-name>jsp</servlet-name>

        <url-pattern>*.jsp</url-pattern>

        <url-pattern>*.jspx</url-pattern>

    </servlet-mapping>

```

该路径下的xxx.jsp请求,都被拦截了,所以404报错,如果使用视图解析器,返回就跳过了拦截器,访问资源

# 1.tomcat中web.xml

```

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<display-name>servyoudemo</display-name>

<welcome-file-list>

<welcome-file>/login.jsp</welcome-file>

</welcome-file-list>

<!-- 加载配pring mvc配置文件 -->

<servlet>

<servlet-name>DispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:/spring/springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<!--

    早期是用*.do或*.Action之类的,来拦截xxx.do的请求,这种,静态资源都不会被拦截,只有指定后缀的url会被拦截;

    后期RES风格,xxx.do的请求就不用了,直接使用xxx/xxx;所以改用 / 来拦截,这用一来,所有的请求都会被spring给加载,静态资源就会出现无法访问的错误;

  /  不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。

  /* 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。

-->

<servlet-mapping>

<servlet-name>DispatcherServlet</servlet-name>

<url-pattern>*.from</url-pattern>

</servlet-mapping>

</web-app>

```

# 2.springmvc.xml

```

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"

      xmlns:util="http://www.springframework.org/schema/util"

      xsi:schemaLocation="http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->

    <context:component-scan base-package="com.liang.servyou.web"/>

    <!-- 注解驱动 映射器和适配器 -->

    <mvc:annotation-driven/>

    <!-- 视图解析 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/" />

        <property name="suffix" value=".jsp" />

    </bean>

    <!-- spring mvc 能够访问静态内容 begin  如果用*.from 或  /  拦截方式,静态资源等就不会被spring拦截,就能正常加载 -->

    <mvc:resources location="/scripts/" mapping="/scripts/**" />

    <mvc:resources location="/WEB-INF/jsp/" mapping="/WEB-INF/jsp/**" />

    <mvc:resources location="/js/" mapping="/js/**" />

    <mvc:resources location="/images/" mapping="/images/**" />

    <!-- spring mvc 能够访问静态内容 end -->

</beans>

```

上一篇 下一篇

猜你喜欢

热点阅读