SpringMVC配置正确,依然无法访问静态资源的问题(编译器IDEA)
IDEA项目结构
静态资源css、fonts、js、images、html文件夹等放在webapp目录下,没毛病。
项目web配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--自定义配置文件路径-->
<init-param>
<!--contextConfigLocation是表示路径的属性-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>applicationContext</servlet-name>
<!--
使用框架的时候, url-pattern可以使用两种值
1. 使用扩展名方式
2.使用斜杠 "/",会导致项目静态资源无法访问404
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
定义了SpringMVC中央调度器,使用了斜杠“/”的方式定义前后端交互的访问地址,没毛病。
Spring配置文件applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:annotation-driven/>
</beans>
应用了mvc:resources标签声明静态资源,并且应用了MVC注解驱动mvc:annotation-driven解决了mvc:resources标签与@RequestMapping注解的冲突问题,没毛病。