spring mvc静态资源加载失败
我想在我的示例spring mvc项目中使用静态资源,在我的项目中,我在资源文件夹下创建了一个主题,如图所示。 spring mvc静态资源加载失败
然后我就加入弹簧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>
WhenI尝试访问静态资源在许多不同的充方式,我不能够访问它。 这是我的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>
任何一个可以帮我解决这个问题呢?
大概是这样的:
您有这方面的配置
<mvc:resources mapping="/resources/**" location="/resources/theme/"
但是,当你调用resourcek,你使用这个
<c:url value="/resources/themes/css/bootstrap.min.css"/
我htink你必须使用这个
/resources/css/bootstrap.min.css
你有3个问题:
- 1:在你的Maven项目,文件夹
src/main/resources
有一个Maven的特殊意义:它的内容将被放置在classpath中编译后。 -
2:当您使用弹簧资源映射时,映射匹配映射部分'**'的url部分将被添加到获取文件的位置部分。 (抱歉不好解释)一个例子更清楚:假设你有这样的资源映射
<mvc:resources mapping="https://stackoverflow.com/a/**" location="/b/c/" >
你请求URL
http://localhost/myApp/a/d
Spring会查找文件在/b/c/d
! - 所以,当你发送一个请求....../resources/themes/css/bootstrap.min.css
春季将搜索:/resources/theme/themes/css/bootstrap.min.css
3:在春天的配置和文件夹使用
theme
但在JSP使用themes
为解决方案1将会是:要么更改location
弹簧资源映射以通过在位置属性中使用classpath:
前缀从类路径中选取资源,要么将文件放在其他文件夹中:source/main/webapp/resources/theme
(然后位置参数/resources/theme/
)将映射。
与类路径修复例如用于和一个可能的修复为:
<mvc:resources mapping="/resources/theme/**"
location="classpath:/resources/theme/" cache-period="31556926"/>
JSP - 固定为:
<link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>
如果资源由一个需要可见前端,你需要把资源放在“webapp”文件夹中。例如:webapp/theme/css/bootstrap.min.css