dispatcherServlet

所有的访问都会经过Spring的dispatcherServlet,如果访问的是静态资源,会出现404错误。
web.xml

  <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-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>
  
 <error-page><!--原生servlet异常处理-->
  			<error-code>404</error-code> <!--404错误时,转发到准备好的404.html-->
  			<location>/static/404.html</location>
  </error-page> 
  

Spring-mvc.xml中配置静态资源目录

配置好静态资源映射(css,js,jpg,gif,视频,html,音频) ,此目录下的静态资源可以被访问到了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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 https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
    <!-- 开启基本注解Controller,Service,Repository,Component,Autowired,Resource,Qulifer -->
	<context:component-scan base-package="com.mvc"></context:component-scan>
	 <!-- 处理器映射器: 识别@RequestMapping注解,对该注解标注的类和方法做映射,url映射 。http://localhost:8080/0622mvc/user/login   把/user/login映射到一个类中一个处理器方法,-->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
  <!--处理器适配器: 当有请求过来,dispatcherServlet找到该请求所映射到的处理器方法,该bean对象负责执行方法。封装参数 -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
  <!-- 视图解析器:当处理器方法执行完成后,配置一个视图路径地址 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  	<property name="prefix" value="/WEB-INF/view/"></property>
  	<property name="suffix" value=".jsp"></property>
  </bean>
    				   	<!-- 上面和之前一样 加了这一句-->
				<!-- 静态资源映射(css,js,jpg,gif,视频,html,音频) -->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
			<!-- 可以有多个-->
		<!-- location:访问路径  mapping:实际路径    可以不一致 -->
<mvc:resources location="/PWP/" mapping="/qwq/**"></mvc:resources>
</beans>

添加的
在标签头中,增加了两句引用:
xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"
以及这个标签

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

/static/** 匹配static目录下所有子文件

静态资源文件夹

在web目录下新建静态资源目录,并在spring-mvc.xml中配置<mvc:resources>标签,可以配置多个目录为静态文件目录。
在这里插入图片描述
404.html

<body>
 访问的资源不存在!404!!404!!
</body>

运行

进入了静态页面404.html
在这里插入图片描述
这里我们访问了一个不存在的地址,触发了原生servlet的异常处理,跳转到了404.html页面。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐