2012-08-13 102 views
1

以下是我的MVC-dispatcher.xml文件Spring MVC的資源未映

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

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/view/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<mvc:annotation-driven /> 
<mvc:resources location="/static/" mapping="/static/**" /> 
<context:component-scan base-package="in.codejava.personal.controllers" /> 
</beans> 

我要去哪裏錯了?所有的靜態/ *網址正在被我創建的404控制器映射,而不是靜態資源。

WEB.XML

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 

<display-name>Personal Web Blogs</display-name> 
<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 
<!-- <servlet-mapping> --> 
<!--  <servlet-name>default</servlet-name> --> 
<!--  <url-pattern>/static/*</url-pattern> --> 
<!-- </servlet-mapping> --> 
<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

如果我刪除註釋部分,它工作正常。

+0

您可能需要包括更多的信息,以幫助調試web.xml文件中dispatcherservlet的相關條目,映射到相同路徑的任何潛在控制器。另外,你能否詳細說明你的404控制器是如何執行的。 – 2012-08-13 20:32:41

+0

@BijuKunjummen增加了Web.XML。 404控制器只是一個@ requestMapping,當沒有映射匹配時執行。 – 2012-08-14 01:06:38

+0

這聽起來很合理,對@Akhil而言,如果你刪除了評論部分,那麼你就說過了。在已註釋的部分中,您正在註冊另一個servlet映射權限 - 這是哪個servlet。 – 2012-08-14 02:04:46

回答

5

我要採取平底船和假設你使用的是傳統的目錄結構爲您的JS,CSS,圖片資源,就像這樣:

  • src/main/webapp/[js|css|images]

在這種情況下,你mvc:resources應該是這樣的:

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

,你應該引用它們在你的JSP是這樣的:"${pageContext.request.contextPath}/static/js/foo.js"

+0

並不重要。檢查WEB.XML和我的描述。 – 2012-08-14 01:07:19

+1

nickdos的回答指出了我在自己的資源映射問題中的正確解決方法。他的jsp contextPath參考是我所忽略的情況中的關鍵。首先檢查基礎知識,對嗎?謝謝。 – 2013-02-02 01:27:01

0

我只是在我的web.xml <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping>

添加一個新的servlet映射,然後可以參考關於我的HTML資源的路徑:

<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen">