我正在尝试在我的示例 spring mvc 项目中使用静态资源,在我的项目中,我在资源文件夹下创建了一个主题,如图所示。
然后我在 spring-mvc-servlet.xml 中添加了以下 3 行代码
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.sudheer.example" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/theme/" cache-period="31556926"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>
当我尝试以多种不同方式访问静态资源时,我无法访问它。这是我的jsp页面:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/resources/themes/css/bootstrap.min.css" rel="stylesheet" >
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/themes/bootstrap.min.css">
<link href="<c:url value="/resources/themes/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div class="btn"></div>
</body>
</html>
这是我的 web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
谁能帮我解决这个问题?