2013-02-07 16 views
9

Sono nuovo in java e primavera. Sto provando a fare ciao mondo app e non capisco cosa sto sbagliando.SpringMVC e risorse statiche

qui è la mia struttura di directory:

test_app 
-pom.xml 
-src 
--main 
---java 
----com.example.web 
-----IndexController.java 
---webapp 
----static 
-----img 
------example.jpg 
----WEB-INF 
-----web.xml 
-----dispatcher-servlet.xml 
-----jsp 
------index.jsp 

e fonti: web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC 
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
     "http://java.sun.com/dtd/web-app_2_3.dtd" > 

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

    <display-name>Movie Reminder WebApp</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
</web-app> 

dispatcher-servlet.xml:

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

    <context:annotation-config/> 
    <context:component-scan base-package="com.example.web"/> 
    <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> 

IndexController.java

012.

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <title>Hello Spring!</title> 
</head> 
<body> 
<img src="<c:url value="/static/img/example.jpg" />" /> 
</body> 
</html> 

quando schiero la mia app e la richiesta, ricevo 404 su richiesta di immagine

http://localhost:8081/ --- http 200 ok 
http://localhost:8081/static/img/example.jpg - http 404 not found 

quando aggiungo MVC: risorse per dispatcher-servlet.xml

<context:annotation-config/> 
<context:component-scan base-package="com.example.web"/> 
<mvc:resources mapping="/static/**" location="/static/" /> 

e ricompilare im ottenere 404 su/richiesta

http://localhost:8081/ --- http 404 not foundok 
http://localhost:8081/static/img/example.jpg - http 200 ok 

Help me please di capire quello che sto facendo male

+0

Im usando Apache Tomcat7 su Windows 7, la primavera-MVC 3.2.1.RELEASE – user2050786

+0

versione Java 1.7.0_11-b21 – user2050786

+0

add nel file di configurazione di primavera – unionx

risposta

0

E 'a causa della mappatura servlet. Tutte le richieste in arrivo vengono indirizzate al servlet. Ma il servlet non sa come onorare le richieste di risorse statiche. È necessario aggiungere una mappatura per le risorse statiche. Esistono diversi approcci:

  1. Utilizzare la modalità fornita dal server web. Sfortunatamente questo varia leggermente a seconda del server che hai.

  2. Utilizzare un servlet che può servire risorse statiche.

Quale server Web stai utilizzando?

+0

Im usando Tomcat7 su Windows7, Spring MVC 3.2 – user2050786

+0

Vedere il rispondi sotto –

6

Basta aggiungere queste due righe al dispatcher-servlet.xml

<mvc:resources mapping="/static/**" location="/static/" /> 
<mvc:default-servlet-handler /> 

Ecco cosa la documentazione per default-servlet-handler dice:

Configura un gestore per il servizio risorse statiche inoltrando al servlet predefinito del contenitore Servlet . L'utilizzo di questo gestore consente di utilizzare utilizzando un mapping "/" con DispatcherServlet mentre si utilizza ancora il contenitore Servlet per servire le risorse statiche. Questo gestore invierà tutte le richieste al Servlet predefinito. Pertanto è importante che rimanga ultimo nell'ordine di tutti gli altri URL HandlerMappings.Ciò si verifica se si utilizza l'elemento "annotation-driven" o se si imposta l'istanza personalizzata di HandlerMapping assicurandosi di impostare la proprietà "order" su un valore inferiore a quello dello DefaultServletHttpRequestHandler, che è Integer.MAX_VALUE.

+0

ho aggiunto a dispatcher-servlet.xml dopo . Ho ancora http: // localhost: 8081/--- http 404 non trovato, http: // localhost: 8081/static/img/example.jpg - http 200 ok – user2050786

+0

Vedere http://stackoverflow.com/editing -help # comment-formattazione. Non utilizzare tag HTML su StackOverflow. – adarshr

0

partire dalla primavera 3.0 e versioni successive è necessario aggiungere in seguito alla configurazione Primavera:

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

Questo dice il tuo servlet dispatcher come risolvere risorse statiche. Spero che questo aiuti.

0

Utilizzare questo, funzionerà always.Good fortuna :)

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/").setCachePeriod(31556926);`enter code here` 
    } 
} 
0

configura il servlet predefinito in web.xml per servire tutte le attività statiche, quindi basta richiesta percorso raggiungerà i controllori:

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>/resources/*</url-pattern> 
</servlet-mapping> 
Problemi correlati