【问题标题】:How do I map static assets in my Spring app?如何在我的 Spring 应用程序中映射静态资产?
【发布时间】:2011-07-06 13:29:42
【问题描述】:

我使用的是 Spring 3.0.5。我的所有静态资产都放在我的网络应用程序根目录下名为“static”的文件夹中(与 WEB-INF 处于同一级别)。如何将“http://mydomain.com/context-path/static/some-asset”形式的 URL 映射到我的“静态”文件夹?

这很复杂,因为我有一个映射到根上下文的视图解析器(来自我的 web.xml)......

 <!-- Declare a Spring MVC DispatcherServlet as usual -->  
 <servlet>  
     <servlet-name>dispatcher</servlet-name>  
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext  
        instead of the default XmlWebApplicationContext -->  
     <init-param>  
         <param-name>contextClass</param-name>  
         <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>  
     </init-param>  
     <load-on-startup>1</load-on-startup>  
 </servlet>  

 <servlet-mapping>  
     <servlet-name>dispatcher</servlet-name>  
     <url-pattern>/</url-pattern>  
 </servlet-mapping>

好的,感谢您的帮助,- 戴夫

PS - 添加 mvc:resources 似乎并没有治愈痛苦。我添加到我的 parentContext.xml 文件中...

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:resources mapping="/static/**" location="/static/"/>

但随后出现异常,“严重:Servlet 调度程序的 Servlet.service() 引发异常 javax.servlet.ServletException:处理程序没有适配器 [com.myco.systems.leadsmonitor.web.controller.HomeController@6870c52d]:您的处理程序是否实现了像 Controller 这样的受支持接口?”当我访问我的主页“/”时。

【问题讨论】:

  • 什么是parentContext.xml?你通常把它放在 dispatcher-servlet.xml

标签: java spring spring-mvc


【解决方案1】:

除了使用&lt;mvc:resources /&gt;,请确保您在同一个文件中有这一行:

<mvc:annotation-driven />

我不知道为什么会这样,但是当你使用 &lt;mvc:resources /&gt; 时,它会禁用 MVC 机制的一些非常重要的部分——我相信尊重你的 Controller 和 RequestMapping 注释。我猜&lt;mvc:annotation-driven /&gt; 告诉你的电脑你真的非常需要它(?)。

祝你好运。我只是在同样的问题上挣扎。这是我最终的样子:

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

    <!-- Explicitly enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <mvc:resources mapping="/static/**" location="/static/" />

    <context:component-scan
        base-package="com.seemikecode.controller" />

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

【讨论】:

    【解决方案2】:

    最好的选择是使用&lt;mvc:resources /&gt;

    【讨论】:

    • 我编辑了我的回复,这样你就可以看到我是如何添加 mvc:resources 的。添加它完全破坏了我的应用程序。我包含了错误消息。
    • @Dave 您可能缺少控制器的 handlerAdapter 配置。
    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多