我在尝试从 jsp 文件 (WEB-INF/jsp/index.jsp) 访问 css 和 js 文件时遇到问题。我通读了 Stack Overflow 上其他人面临的一些类似问题。
我将这行代码添加到我的 SpringMVCProj-servlet.xml 中,它使事情变得更加复杂。
<mvc:resources mapping="/WebContent/**" location="/WebContent/" />
SpringMVCProj-servlet.xml
<context:component-scan base-package="com.myapp" />
<mvc:resources mapping="/WebContent/**" location="/WebContent/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
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" id="WebApp_ID" version="2.5">
<display-name>Spring MVC Application</display-name>
<welcome-file-list>
<!-- <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
-->
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVCProj</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCProj</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVCProj-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
这是在我添加代码行之前。
<mvc:resources mapping="/WebContent/**" location="/WebContent/" />
这之后...