2010-01-24 2 views
2

Essayer de configurer le printemps 3 MVC, voici ce que je l'ai fait jusqu'à présent:ESSAYER Spring MVC 3, Obtenir une page d'erreur

J'ai ajouté tout le printemps 3.0 bibliothèques à mon projet NetBeans.

Mon web.xml est:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <servlet> 
     <servlet-name>WebApplication1</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>WebApplication1</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping>  
</web-app> 

Ensuite, je créé un WebAppliation1-servlet.xml, aussi dans mon dossier/web-inf:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
- DispatcherServlet application context for PetClinic's web tier. 
--> 
<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:oxm="http://www.springframework.org/schema/oxm" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 

<!-- 
    - The controllers are autodetected POJOs labeled with the @Controller annotation. 
--> 
<context:component-scan base-package="org.springframework.Hi"/> 

<!-- 
    - The form-based controllers within this application provide @RequestMapping 
    - annotations at the type level for path mapping URLs and @RequestMapping 
    - at the method level for request type mappings (e.g., GET and POST). 
    - In contrast, ClinicController - which is not form-based - provides 
    - @RequestMapping only at the method level for path mapping URLs. 
    - 
    - DefaultAnnotationHandlerMapping is driven by these annotations and is 
    - enabled by default with Java 5+. 
--> 



<!-- 
    - This bean resolves specific types of exceptions to corresponding logical 
    - view names for error views. The default behaviour of DispatcherServlet 
    - is to propagate all exceptions to the servlet container: this will happen 
    - here with all other types of exceptions. 
--> 
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
    <property name="exceptionMappings"> 
    <props> 
    <prop key="org.springframework.web.servlet.PageNotFound">pageNotFound</prop> 
    <prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop> 
    <prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop> 
    </props> 
    </property> 
</bean> 


<!-- 
    - The BeanNameViewResolver is used to pick up the visits view name (below). 
    - It has the order property set to 2, which means that this will 
    - be the first view resolver to be used after the delegating content 
    - negotiating view resolver. 
    --> 
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/> 
<!-- 

    - This bean configures the 'prefix' and 'suffix' properties of 
    - InternalResourceViewResolver, which resolves logical view names 
    - returned by Controllers. For example, a logical view name of "vets" 
    - will be mapped to "/WEB-INF/jsp/vets.jsp". 
--> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" 
    p:suffix=".jsp" p:order="2"/> 



</beans> 

J'ai ensuite créé un accueil simple. jsp dans le dossier /WEB-INF/jsp/welcome.jsp

Je crée alors un paquet:

org.springfra mework.Hi

et moi avons créé un fichier TestController.java:

package org.springframework.Hi;

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 


@Controller 
public class TestController { 


    @RequestMapping("/") 
    public String welcomeHandler(){ 
     return "welcome"; 
    } 

} 

Je voulais que cela fonctionne w/o toutes les extensions de fichiers, donc je ne pas mettre fin à mes urls avec xxx.htm etc.

Alors, quand je lance le projet NetBeans je reçois l'erreur :

type Status report 

message Servlet WebApplication1 is not available 

description The requested resource (Servlet WebApplication1 is not 

available) is not available.

Que manque-t-il exactement ici?

journaux Tomcat

Merci pour les commentaires

Mes journaux tomcat montrent ceci:

SEVERE: Servlet /WebApplication1 threw load() exception 
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 
     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387) 
     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) 
     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316) 
     at org.springframework.web.servlet.DispatcherServlet.<clinit>(DispatcherServlet.java:207) 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
+0

Journaux d'application? catalina.out? –

+0

Les journaux ont aidé, voir la réponse révisée – skaffman

Répondre

4

J'Asuming ce message d'erreur que vous avez cité était le message d'erreur du navigateur voit quand il essaie d'accéder à l'application. C'est parce qu'il ne s'est pas déployé correctement.

Vous devez parcourir les journaux de démarrage du serveur Web, ce qui vous donnera beaucoup plus d'informations sur les raisons pour lesquelles il n'a pas été déployé.

edit: OK, ça aide beaucoup ... vous devez ajouter Apache Commons Logging à votre classpath.

+0

@skaffman J'ai trouvé les fichiers journaux (appris quelque chose de nouveau aujourd'hui), il semble qu'il ne peut pas trouver une classe. – Blankman

+0

En fait, j'ai dû ajouter le téléchargement et ajouter la bibliothèque de journalisation apache commons à mon projet. Où exactement est-il référencé? Je suppose que spring mvc l'utilise par défaut? (la page fonctionne, merci!) – Blankman

+0

Oui, Spring l'utilise – skaffman

0

Je rencontrais un problème similaire ... sauf que cela n'arrivait que lorsque j'essayais d'exécuter l'application Spring Web MVC via l'IDE STS (version 2.3.2). Cependant, si je m'étais empaqueté en tant que fichier WAR et que je l'ai déployé sur une instance en cours de tcServer ou même Tomcat, l'application fonctionnait très bien.

J'ai fini par ajouter la consignation-journalisation et d'autres fichiers JAR à la configuration de lancement du serveur dans STS IDE, dans la section Entrées utilisateur sous l'onglet Chemin de classe. Cela a résolu mon problème. Pourquoi certains JAR ne sont pas ajoutés à la dépendance du module Java EE lors de l'exécution dans STS, je ne comprends toujours pas.

Espérons que cela aide.