1

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

谁能帮我解决这个问题?

4

4 回答 4

4

你有3个问题:

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

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

    你请求 urlhttp://localhost/myApp/a/d然后 spring 会在/b/c/d! - 因此,当您发送....../resources/themes/css/bootstrap.min.cssSpring 请求时,会搜索:/resources/theme/themes/css/bootstrap.min.css

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


1的解决方案是:要么更改locationspring 资源映射以通过使用classpath:location 属性中的前缀从类路径中获取资源,要么将文件放在其他文件夹中:(source/main/webapp/resources/theme然后是 location 参数/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"/>
于 2015-11-06T20:19:40.377 回答
2

大概是这样的:

你有这个配置

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

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

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

我觉得你必须用这个

/resources/css/bootstrap.min.css
于 2015-11-06T20:16:28.517 回答
1

我在这里为 Spring MVC 创建了一个框架项目(也使用了静态资源) 。

于 2018-01-10T06:47:19.843 回答
1

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

于 2015-11-06T20:37:52.380 回答