2011-07-06 22 views
4

我使用Spring 3.0.5。我將所有靜態資產放在我的web-app根目錄下的一個名爲「static」的文件夾中(與WEB-INF處於同一級別)。如何將表單「http://mydomain.com/context-path/static/some-asset」的URL映射到我的「靜態」文件夾?如何在我的Spring應用程序中映射靜態資產?

這是事實,我有一個映射到根上下文(從我的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:資源似乎無法彌補痛苦。我加入到我的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.service()進行servlet調度拋出異常 javax.servlet.ServletException:無適配器處理程序[COM .myco.systems.leadsmonitor.web.controller.HomeController @ 6870c52d]:你的處理程序是否實現了像Controller這樣的受支持的接口?「當我訪問我的主頁「/」。

+0

什麼是parentContext.xml?你通常把它放在dispatcher-servlet.xml中 – Bozho

回答

4

除了使用<mvc:resources />,請確保您有這條線在同一個文件:

<mvc:annotation-driven /> 

我不知道爲什麼會發生,但是當你使用<mvc:resources />,它禁用MVC機制的一些非常重要的部分 - 尊重你的Controller和RequestMapping註解我相信。我想<mvc:annotation-driven />告訴你的電腦,你真的,真的需要它(?)。

祝你好運。我只是努力解決同樣的問題。這是我最後的樣子:

<?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> 
4

的最佳選擇是使用<mvc:resources />

+0

我編輯了我的回覆,這樣你就可以看到我是如何添加mvc:resources的。添加它完全打破了我的應用程序我收到了錯誤消息。 – Dave

+0

@Dave您可能錯過了控制器的handlerAdapter配置。 – Bozho

相關問題