spring的静态资源访问
方式一:使用mvc的default-servlet-handler
spring.xml中配置
<mvc:annotation-driven />
<mvc:default-servlet-handler />
方式二:使用mvc的resources
spring.xml中配置
<mvc:annotation-driven />
<mvc:resources mapping = "/images/**" location = "/images/" />
<mvc:resources mapping = "/html/**" location = "/html/" />
<mvc:resources mapping = "/js/**" location = "/js/" />
方式三:激活tomcat的默认调度器default
要写在DispatcherServlet的前面, 让defaultServlet先拦截
web.xml中配置
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
二、springboot
我们习惯会把静态资源放在classpath:/static/目录下
classpath:就是resources
访问的路径 开头必须是绝对路径 / 比如: /css/xx.css 会自动进入 上面那些目录中找
默认的静态资源路径为:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
访问优先级从高到低
META-INF/resources > resources > static > public