[英]Not able to load static resources in Spring
My question is simple. 我的问题很简单。 I am simply not able to load static resources in Spring Below are my configuration files.
我根本无法在Spring中加载静态资源。以下是我的配置文件。
dispatcher-servlet.xml 调度员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 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>
I have put my js and css files in /webapp/resources/css and /webapp/resources/js. 我已经将我的js和css文件放在/ webapp / resources / css和/ webapp / resources / js中。
I know this question has been addresses many times but I am simply not able to load static resources. 我知道这个问题已经解决了很多次,但是我根本无法加载静态资源。 I get 405 no method error on browser.
我在浏览器上得到405 no method error。
Tried every other solution on net. 在网上尝试了所有其他解决方案。 Kindly tell where am I going wrong.
请告诉我我要去哪里错了。 Really frustrated with these spring configuration files.
这些弹簧配置文件让我非常沮丧。
You are providing applicationContext*.xml
whereas your name of context file is dispatcher-servlet.xml
您提供的是
applicationContext*.xml
而上下文文件的名称为dispatcher-servlet.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param- value>
</context-param>
Moreover you have to read about usage of <mvc:default-servlet-handler/>
and <mvc:resources>
, from the documentation as both have a different way to serve static content, as follows: 此外,您还必须从文档中了解
<mvc:default-servlet-handler/>
和<mvc:resources>
用法,因为它们都有不同的方式来提供静态内容,如下所示:
With <mvc:default-servlet-handler/>
spring dispatcher serve static resources under the web root (/webapp) using default servlet. 通过
<mvc:default-servlet-handler/>
spring dispatcher使用默认servlet 在Web根(/ webapp)下提供静态资源 。
And with <mvc:resources>
spring uses a convenient way to serve static resources from locations other than the web application root, including locations on the classpath. 通过
<mvc:resources>
spring使用一种便捷的方式从Web应用程序根目录以外的位置(包括类路径上的位置)提供静态资源。
Talking about your configuration, as you have do not have static resources under the web root, as you mentioned: 谈到您的配置,正如您提到的那样,您在Web根目录下没有静态资源:
I have put my js and css files in /webapp/resources/css and /webapp/resources/js.
我已经将我的js和css文件放在/ webapp / resources / css和/ webapp / resources / js中。
You have to edit the configuration to use either one of them to serve static files and have to access in a specific way, as follows: 您必须编辑配置以使用其中之一来提供静态文件,并且必须以特定方式进行访问,如下所示:
Considering you are using convenient way to serve static file, as below: 考虑到您正在使用一种方便的方式来提供静态文件,如下所示:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
Then you have to access CSS
and JS
providing a path to a folder where /resources
is the mapping attribute in mvc:resources
tag: 然后,您必须访问
CSS
和JS
并提供指向文件夹的路径,其中/resources
是mvc:resources
标记中的映射属性 :
<link href="<c:url value="/resources/css/my.css" />" rel="stylesheet">
<script src="<c:url value="/resources/js/my.js" />"></script>
Above snippet assume you have my.css
and my.js
in respective folder. 以上代码段假设您在各自的文件夹中都有
my.css
和my.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.