IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Web Java Discussion :

Configuration de Spring MVC grâce à une classe java


Sujet :

Spring Web Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 145
    Points : 62
    Points
    62
    Par défaut Configuration de Spring MVC grâce à une classe java
    Bonjour,

    J'essaie de configurer mon projet SpringMVC grâce à des classes java ( donc pas de fichiers xml, web.xml, etc). Quand j’exécute mon projet le serveur ne signale aucune erreur mais cela ne marche pas. je cherche le chic sans trouver. merci de votre aide.
    Voici mes différentes classes.

    Ma classe de configuration
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    package template.Configuration;
     
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
     
    @Configuration
    @ComponentScan( { "template.*" } )
    public class ApplicationConfig {
     
        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass( JstlView.class );
            viewResolver.setPrefix( "/WEB-INF/views/" );
            viewResolver.setSuffix( ".jsp" );
            return viewResolver;
        }
     
    }
    Ma classe d'initialisation de spring mvc
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    package template.Configuration;
     
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
     
    public class SpringMVCWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
     
        @Override
        protected Class<?>[] getRootConfigClasses() {
     
            return new Class[] { ApplicationConfig.class };
        }
     
        @Override
        protected Class<?>[] getServletConfigClasses() {
     
            return null;
        }
     
        @Override
        protected String[] getServletMappings() {
     
            return new String[] { "/" };
        }
     
    }
    Enfin mon controler
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
     
    @Controller
    public class WebController {
     
        @RequestMapping( value = { "/", "/index**" }, method = RequestMethod.GET )
        public ModelAndView index() {
            ModelAndView model = new ModelAndView();
            model.addObject( "message", "template ok!!" );
            model.setViewName( "index" );
            return model;
     
        }
     
    }
    Le rendu du serveur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
     
    juil. 19, 2016 1:39:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:projetWebTemplateJavaConfigAnnotation' did not find a matching property.
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server version:        Apache Tomcat/8.0.28
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server built:          Oct 7 2015 18:25:21 UTC
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server number:         8.0.28.0
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: OS Name:               Linux
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: OS Version:            4.2.0-30-generic
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Architecture:          amd64
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Java Home:             /usr/lib/jvm/java-7-openjdk-amd64/jre
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: JVM Version:           1.7.0_101-b00
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: JVM Vendor:            Oracle Corporation
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: CATALINA_BASE:         /local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: CATALINA_HOME:         /local/home/fnjiokou/workspace/apache-tomcat-8.0.28
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dcatalina.base=/local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dcatalina.home=/local/home/fnjiokou/workspace/apache-tomcat-8.0.28
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dwtp.deploy=/local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Djava.endorsed.dirs=/local/home/fnjiokou/workspace/apache-tomcat-8.0.28/endorsed
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dfile.encoding=UTF-8
    juil. 19, 2016 1:39:24 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFOS: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib32:/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
    juil. 19, 2016 1:39:24 PM org.apache.coyote.AbstractProtocol init
    INFOS: Initializing ProtocolHandler ["http-nio-8081"]
    juil. 19, 2016 1:39:24 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFOS: Using a shared selector for servlet write/read
    juil. 19, 2016 1:39:24 PM org.apache.coyote.AbstractProtocol init
    INFOS: Initializing ProtocolHandler ["ajp-nio-8009"]
    juil. 19, 2016 1:39:24 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFOS: Using a shared selector for servlet write/read
    juil. 19, 2016 1:39:24 PM org.apache.catalina.startup.Catalina load
    INFOS: Initialization processed in 899 ms
    juil. 19, 2016 1:39:24 PM org.apache.catalina.core.StandardService startInternal
    INFOS: Démarrage du service Catalina
    juil. 19, 2016 1:39:24 PM org.apache.catalina.core.StandardEngine startInternal
    INFOS: Starting Servlet Engine: Apache Tomcat/8.0.28
    juil. 19, 2016 1:39:25 PM org.apache.jasper.servlet.TldScanner scanJars
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    juil. 19, 2016 1:39:26 PM org.apache.jasper.servlet.TldScanner scanJars
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    juil. 19, 2016 1:39:26 PM org.apache.catalina.core.ApplicationContext log
    INFOS: 1 Spring WebApplicationInitializers detected on classpath
    juil. 19, 2016 1:39:27 PM org.apache.catalina.core.ApplicationContext log
    INFOS: Initializing Spring root WebApplicationContext
    log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    juil. 19, 2016 1:39:27 PM org.apache.catalina.core.ApplicationContext log
    INFOS: Initializing Spring FrameworkServlet 'dispatcher'
    juil. 19, 2016 1:39:28 PM org.apache.coyote.AbstractProtocol start
    INFOS: Starting ProtocolHandler ["http-nio-8081"]
    juil. 19, 2016 1:39:28 PM org.apache.coyote.AbstractProtocol start
    INFOS: Starting ProtocolHandler ["ajp-nio-8009"]
    juil. 19, 2016 1:39:28 PM org.apache.catalina.startup.Catalina start
    INFOS: Server startup in 3543 ms

  2. #2
    Membre confirmé Avatar de freddou17
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2013
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2013
    Messages : 341
    Points : 566
    Points
    566
    Par défaut
    Salut, il ne manquerais pas un extends

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    @Configuration
    @ComponentScan( { "template.*" } )
    public class ApplicationConfig extends WebMvcConfigurerAdapter{

    Sinon es tu sur d'être en servlet 3 ou 3+

    ++
    "Aucun de nous ne sait ce que nous savons tous, ensemble."
    Lien vers mon appli Funcash n'hésitez pas à donner votre avis

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 145
    Points : 62
    Points
    62
    Par défaut
    J'ai rajouté
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     public class ApplicationConfig extends WebMvcConfigurerAdapter
    et je suis bien en servlet 3.0.1, mais rien n'y fait

  4. #4
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 936
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 936
    Points : 4 356
    Points
    4 356
    Par défaut
    A première vue et sans avoir trop creusé votre code, il manque un @EnableWebMvc quelque part
    et vu le nom de votre classe "ApplicationConfig" vous me semblez mélanger les notions de contexte de l'application et de contexte de la servlet.

Discussions similaires

  1. Réponses: 1
    Dernier message: 27/04/2006, 18h22
  2. Réponses: 3
    Dernier message: 09/04/2006, 13h04
  3. appel d'une class java en javascript ou autre
    Par tit_oune dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 04/04/2006, 15h24
  4. [Tomcat]Executer une classe JAVA au demarrage de TOMCAT
    Par dehbi dans le forum Tomcat et TomEE
    Réponses: 2
    Dernier message: 18/08/2005, 14h23
  5. Réponses: 13
    Dernier message: 25/03/2005, 11h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo