5

Sono assolutamente principiante con Java e Spring e voglio imparare dall'esempio.Nessun mapping trovato per richiesta HTTP con URI [/ myproject /] in DispatcherServlet con nome 'appServlet'

sto usando un out-of-the-box di configurazione/installazione

  • Mac OSX
  • SpringSource Tool Suite come IDE
  • Primavera 2.8.1.RELEASE
  • vFabric tc- -server-developer-2.6.1.RELEASE

Ho provato a generare un nuovo progetto basato su "Spring Template Project". Quindi ho scelto il "Progetto Spring MVC". Il progetto di esempio è generato. Dopo di ciò, senza modificare nulla, ho provato ad eseguire la pagina "home.jsp" tramite "Esegui come". Il server Web viene avviato e, infine, ho ricevuto l'errore nella scheda della console.

alcun mapping trovato per la richiesta HTTP con URI [/ myproject /] in DispatcherServlet con il nome 'appServlet'

E questa altra uscita in queste pagine web:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

enter image description here

Qui potete vedere un'immagine di come il mio progetto è strutturato (auto generato per STS):

enter image description here

Cosa c'è di sbagliato?

Qui potete vedere il contenuto del file diweb.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

Il file root-context.xml ha questo contenuto.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    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"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 

</beans> 

E infine la servlet-context.xml ha questo contenuto.

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    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.0.xsd 
     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"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

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

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.mycompany.myapp" /> 

</beans:beans> 

Qualcuno ha un'idea per risolverlo?

risposta

0

Tutto in WEB-INF non è accessibile dall'esterno e il punto di un framework MVC è di richiamare un controller prima di passare alla vista, quindi non è possibile richiamare direttamente il JSP.

E probabilmente non hai alcun controller Spring mappato all'URL di root, quindi ovviamente non c'è nulla nell'URL http://localhost:8080/myproject/.

+0

@AlfonsoDominguez @duffymo Il progetto generato automaticamente ha un file chiamato "HomeController.java" con il seguente contenuto: ' pacchetto com.mycompany.myapp; [...] PARTE OMITATA [...] /** * Gestisce le richieste per la pagina iniziale dell'applicazione. */ @Controller public class HomeController { \t \t \t/** \t * Semplicemente seleziona la vista di casa di rendere restituendo il suo nome. \t */ \t @RequestMapping (value = "/", method = RequestMethod.GET) \t casa public String (locale locale, il modello Model) { \t \t String str = "messaggio Server"; \t \t \t \t model.addAttribute ("serverTime", str); \t \t \t \t ritorno "home"; \t} \t } ' E 'questo quello che dici? –

+0

sì, questo è quello che intendevo, hai provato ad accedere all'URL 'http: // localhost: 8080/myproject/home'? Quell'URL sembra essere quello corretto dai tuoi file di configurazione. –

1

La convenzione di primavera presuppone che il <servlet-name> nel web.xml per DispatcherServlet corrisponde all'inizio del file XML del contesto di servlet Spring. Rinominare servlet-context.xml a appServlet-context.xml e vedere se questo aiuta.

+0

Provando che ho avuto lo stesso errore. Infatti, nel file web.xml, c'è una riga che specifica il nome del servlet ... Ora ho cambiato per quello che hai suggerito (più il nome del file) ... ma il risultato è lo stesso . \t \t \t contextConfigLocation \t \t \t /WEB-INF/spring/appServlet/appServlet-context.xml

0

Aggiungere un controller all'applicazione che restituisce un'istanza ModelAndView con il nome "home". Quindi configura la mappatura del gestore su quel controller e prova ad accedere alla tua app web con un URL simile a questo: http://localhost:8080/myproject/home.do. Ulteriori informazioni possono essere trovate here e here.