【问题标题】:Not able to load static resources in Spring无法在 Spring 中加载静态资源
【发布时间】:2017-01-23 01:50:57
【问题描述】:

我的问题很简单。我根本无法在 Spring 中加载静态资源 以下是我的配置文件。

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd                 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">


 <!-- Turns on support for mapping requests to Spring MVC @Controller methods
     Also registers default Formatters and Validators for use across all @Controllers -->
<mvc:annotation-driven/>


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/resources/" mapping="/resources/**"/>

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource 
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
<context:component-scan base-package="com.redefine.entertainment"></context:component-scan>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

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>test</display-name>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-   value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>RedefineEntertainment</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>RedefineEntertainment</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我已将我的 js 和 css 文件放在 /webapp/resources/css 和 /webapp/resources/js 中。

我知道这个问题已经解决了很多次,但我根本无法加载静态资源。我在浏览器上收到 405 no method 错误。

在网上尝试了所有其他解决方案。请告诉我哪里出错了。对这些 spring 配置文件感到非常沮丧。

【问题讨论】:

  • 问题已解决。我在eclipse中创建了我的项目,并在其中配置了spring。可能存在一些配置冲突,不允许加载我的静态资源。将我的项目移至 STS(Spring 工具套件),现在我可以引用我的静态资源了。

标签: java spring spring-mvc


【解决方案1】:

您提供的是applicationContext*.xml,而您的上下文文件的名称是dispatcher-servlet.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-   value>
</context-param>

此外,您还必须阅读文档中关于 &lt;mvc:default-servlet-handler/&gt;&lt;mvc:resources&gt; 的用法,因为两者都有不同的方式来提供静态内容,如下所示:

使用 &lt;mvc:default-servlet-handler/&gt; spring dispatcher 使用默认 servlet 在 Web 根目录 (/webapp) 下提供静态资源

通过 &lt;mvc:resources&gt;,spring 使用一种方便的方式从 Web 应用程序根目录以外的位置(包括类路径上的位置)提供静态资源。

谈谈你的配置,因为你在web根目录下没有静态资源,正如你提到的:

我已将我的 js 和 css 文件放在 /webapp/resources/css 和 /webapp/resources/js.

您必须编辑配置以使用其中任何一个来提供静态文件,并且必须以特定方式访问,如下所示:

考虑到您正在使用方便的方式来提供静态文件,如下所示:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/resources/" mapping="/resources/**"/>

然后您必须访问CSSJS 提供一个文件夹的路径,其中/resourcesmvc:resources 标签中的映射属性:

<link href="<c:url value="/resources/css/my.css" />" rel="stylesheet">
<script src="<c:url value="/resources/js/my.js" />"></script>

以上 sn-p 假设您在各自的文件夹中有 my.cssmy.js

【讨论】:

  • 我的意思是调度程序 servlet。项目名称-servlet.xml。那工作得很好。我能够调用我的其他控制器,只面临静态资源的问题。而不是加载它,而是尝试映射控制器并因此得到该错误。
猜你喜欢
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 2019-01-31
  • 2016-08-17
  • 1970-01-01
  • 2022-09-30
相关资源
最近更新 更多