[英]Not able to load static resources in Spring
我的问题很简单。 我根本无法在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 error。
在网上尝试了所有其他解决方案。 请告诉我我要去哪里错了。 这些弹簧配置文件让我非常沮丧。
您提供的是applicationContext*.xml
而上下文文件的名称为dispatcher-servlet.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param- value>
</context-param>
此外,您还必须从文档中了解<mvc:default-servlet-handler/>
和<mvc:resources>
用法,因为它们都有不同的方式来提供静态内容,如下所示:
通过<mvc:default-servlet-handler/>
spring dispatcher使用默认servlet 在Web根(/ webapp)下提供静态资源 。
通过<mvc:resources>
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/**"/>
然后,您必须访问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>
以上代码段假设您在各自的文件夹中都有my.css
和my.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.