2012-10-11 76 views
8

我知道這個問題已被多次詢問,但我無法弄清楚問題所在。我有src/main/webapp文件夾下的images文件夾(這是一個maven web項目)。我在src/main/webapp/WEBINF/views文件夾中有index.jsp。在Spring MVC中不顯示圖像

我試圖訪問圖像和其他資源,如CSS和JS是這樣的:

<img src="/images/left_arrow.png" alt="" />

但卻沒有顯示的圖像。

這裏是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"> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

這裏是WEB-INF/MVC-調度-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
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.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:component-scan base-package="com.ravi.WebApp" /> 

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

</beans> 

這裏是控制器 包com.ravi。 Web應用程序;

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public class HelloController { 

@RequestMapping("/") 
public String printWelcome(Model model) { 
    return "index"; 

} 

} 

回答

11

嘗試添加以下資源聲明Spring配置:

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory --> 
<resources mapping="/images/**" location="/images/" />  

或者,更常見的,是有一個包含所有資源(圖片,CSS,JS等一resources文件夾...),通過子目錄分解。

那麼你的配置是這樣的:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

和你的資源將如下參考:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" /> 
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script> 
<img src="<c:url value="/resources/images/left_arrow.png" />" alt="" /> 
+0

我在Spring配置中添加資源聲明之後得到這個錯誤「沒有找到映射與URI [/ Web應用程序/]在DispatcherServlet的HTTP請求名爲‘MVC-調度’ 「 – Ravi

+0

它的工作。我所做的更改是我在jsp中使用c:url標記「alt =」「/>引用圖像。感謝您的幫助。 – Ravi

0

如果你想保持WEB-INF文件夾外的靜態資源在web根目錄並希望容器處理靜態資源請求,則應將其添加到您的應用程序上下文文件中:

<mvc:default-servlet-handler /> 

@BeauGrantham添加資源映射的建議也可行。

1

你只需要添加一個引用上Spring MVC的配置文件

WEB-INF /彈簧context.xml的圖像文件夾:

<mvc:resources mapping="/images/*" location="/images/" /> 
2

如果使用註釋,然後確保用戶

<mvc:annotation-driven/> 

與資源

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

其他註釋控制器不會工作

0

上述建議對我也有效。但是,如果任何人有相關的名稱空間的麻煩,我不得不MVC的部分添加到mvc-dispatcher-serlvet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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.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 "> 

... 

<mvc:annotation-driven /> 
<mvc:resources mapping="/images/**" location="/images/" /> 
1

請遵循在這張照片的步驟.. :)

步驟1:創建Web應用程序中的一個文件夾但不在WEB-INF中

創建資源然後圖像然後存儲您的圖像。 的webapp /資源/圖像/ fileName.jpg

第2步:現在你已經創建了文件夾

讓我們映射你servlet的配置文件中創建,我們處理的映射路徑路徑 添加以下代碼: <mvc:resources mapping="/resources/*" location="/resources/" />

第3步: 添加代碼,從你在ST創建的位置訪問圖像資源EP 1: <img src="/attendance/resources/images/logo.png" width="100px" height="100px">

Spring MVC access Image in JSP

+0

如果您可以簡要描述每個步驟,它可能會很有用。 – lmiguelvargasf