【问题标题】:spring mvc static resources failed to loadspring mvc静态资源加载失败
【发布时间】:2015-11-06 19:04:18
【问题描述】:

我正在尝试在我的示例 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>

谁能帮我解决这个问题?

【问题讨论】:

    标签: spring spring-mvc spring-security static-resource


    【解决方案1】:

    你有 3 个问题:

    • 1:在你的Maven项目中,文件夹src/main/resources对Maven有特殊的意义:它的内容会在编译后放到classpath中。
    • 2:当你使用spring资源映射时,匹配映射部分'**'的映射部分的url将被添加到获取文件的位置部分。 (抱歉解释不好)一个例子会更清楚:假设你有这个资源映射

       <mvc:resources mapping="/a/**" location="/b/c/" >
      

      如果您请求 url http://localhost/myApp/a/d,那么 spring 将在 /b/c/d 处查找文件! - 所以当你发送....../resources/themes/css/bootstrap.min.css 的请求时,Spring 会搜索:/resources/theme/themes/css/bootstrap.min.css

    • 3:在 spring 配置和文件夹中,您使用 theme,但在 jsp 中,您使用 themes


    1 的解决方案是:要么更改 location spring 资源映射以通过在 location 属性中使用 classpath: 前缀从类路径中获取资源,要么将文件放在另一个文件夹:source/main/webapp/resources/theme(然后位置参数/resources/theme/)将映射。

    1 的类路径修复示例和 2 的一种可能修复:

    <mvc:resources mapping="/resources/theme/**"
        location="classpath:/resources/theme/" cache-period="31556926"/>
    

    jsp - 修复 3

    <link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>" 
           rel="stylesheet"  type="text/css"/>
    

    【讨论】:

      【解决方案2】:

      大概是这样的:

      你有这个配置

      <mvc:resources mapping="/resources/**" location="/resources/theme/"
      

      但是当你调用resourcek时,你使用这个

      <c:url value="/resources/themes/css/bootstrap.min.css"/
      

      我觉得你必须使用这个

      /resources/css/bootstrap.min.css
      

      【讨论】:

        【解决方案3】:

        如果需要前端可见的资源,您需要将资源放在“webapp”文件夹中。示例:webapp/theme/css/bootstrap.min.css

        【讨论】:

          【解决方案4】:

          我已经为 Spring MVC 创建了一个骨架项目(也使用静态资源)here

          【讨论】:

            猜你喜欢
            • 2022-09-30
            • 2020-01-18
            • 2019-01-31
            • 1970-01-01
            • 2014-10-15
            • 1970-01-01
            • 2023-04-01
            • 1970-01-01
            • 2016-09-19
            相关资源
            最近更新 更多