2012-10-11 21 views
8

So che questa domanda è stata posta molte volte, ma non sono in grado di capire quale sia il problema. Ho la cartella delle immagini sotto la cartella src/main/webapp (questo è un progetto web maven). Ho index.jsp nella cartella src/main/webapp/WEBINF/views.Non visualizzare le immagini in Spring MVC

sto cercando di accedere alle immagini ed altre risorse, come CSS e JS come questo:

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

Ma le immagini non sono sempre visualizzati.

Ecco il file 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> 

Qui è la WEB-INF file/MVC-dispatcher-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> 

Ecco il controller pacchetto com.ravi. WebApp;

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"; 

} 

} 

risposta

11

Prova ad aggiungere la seguente dichiarazione risorse alla configurazione Primavera:

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

In alternativa, e più comune, è quello di avere una cartella resources che contiene tutte le risorse (immagini, CSS, JS, etc ...), suddiviso per sottodirectory.

La configurazione sarebbe quindi simile:

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

e le vostre risorse sarebbe fatto riferimento come segue:

<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

sto ottenendo questo errore dopo aver aggiunto la dichiarazione di risorse in Configurazione Primavera "alcun mapping trovato per la richiesta HTTP con URI [/ WebApp /] in DispatcherServlet con il nome 'MVC-dispatcher' " – Ravi

+0

Ha funzionato. Il cambiamento che ho apportato è che ho fatto riferimento alle immagini nel jsp usando c: url tag "alt =" "/>. Grazie per l'aiuto – Ravi

0

Se si desidera mantenere le risorse statiche di fuori delle cartelle WEB-INF nella vostra web principale e vogliono il contenitore per gestire le richieste di risorse statiche, si dovrebbe aggiungere questo alla vostra applicazione contesto del file:

<mvc:default-servlet-handler /> 

Anche il suggerimento di @BeauGrantham per aggiungere la mappatura delle risorse funzionerà.

1

Hai solo bisogno di aggiungere un riferimento vostra cartella di immagini su Spring MVC file di configurazione

WEB-INF/primavera-context.xml:

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

Se Utilizzando annotazione quindi assicurarsi di utente

<mvc:annotation-driven/> 

con risorse

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

altro controllore annotaion non funziona

0

I suggerimenti sopra riportati hanno funzionato anche per me.Ma se qualcun altro ha problemi con lo spazio dei nomi associato, ho dovuto aggiungere la parte di MVC per 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

Si prega di seguire la procedura descritta in questa immagine .. :)

Fase 1: Creare una cartella in webapp ma non in WEB-INF

Creare risorse quindi immagini quindi memorizzare l'immagine. webapp/resources/images/filename.jpg

Fase 2: Ora che hai creato le cartelle

Fateci mappare il percorso è stato creato nel file di configurazione servlet in cui abbiamo a che fare con la mappatura di i percorsi aggiungere questo codice: <mvc:resources mapping="/resources/*" location="/resources/" />

Fase 3: Aggiungere il codice per accedere alla risorsa immagine dalla posizione che si è creato in v ep 1: <img src="/attendance/resources/images/logo.png" width="100px" height="100px">

Spring MVC access Image in JSP

+0

potrebbe essere utile se si potesse descrivere brevemente ogni passaggio. – lmiguelvargasf

Problemi correlati