Spring 3 MVC hello world exemple

Spring 3 MVC hello world example

Dans ce didacticiel, nous vous montrons un exemple de monde bonjour Spring 3 MVC, utilisant l'outil de construction Maven.

Technologies utilisées:

  1. Spring 3.2.13.RELEASE

  2. Maven 3

  3. JDK 1.6

  4. Eclipse 4.4

  5. Boostrap 3

Spring 4 MVC XML
Essayez ceciSpring 4 MVC hello world example.

Spring 3 MVC Annotation
Essayez ceciSpring 3 MVC hello world annotation example.

1. Structure du projet

Téléchargez le projetsource code et vérifiez la structure des dossiers du projet:

spring3-mvc-maven-project

2. Maven

Un modèlepom.xml pour démarrer rapidement un projet Spring MVC, il définit les dépendances Spring 3, un conteneur Jetty intégré et la configuration de l'espace de travail Eclipse.

pom.xml


    4.0.0
    com.example
    spring3-web
    war
    1.0-SNAPSHOT
    spring css

    
        1.6
        3.2.13.RELEASE
        1.2
    

    
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            javax.servlet
            jstl
            ${jstl.version}
        
    

    
      
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.3
            
                ${jdk.version}
                ${jdk.version}
            
        

        
        
            org.eclipse.jetty
            jetty-maven-plugin
            9.2.11.v20150529
            
                10
                
                    /spring3
                
            
        

        
        
            org.apache.maven.plugins
            maven-eclipse-plugin
            2.9
            
                true
                true
                2.0
                spring3
            
        
      
    

  

3. Contrôleur et cartographie

Le@RequestMapping est disponible depuis 2.5, mais maintenant amélioré pour prendre en charge les URL de style REST.

HelloController.java

package com.example.web.controller;

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 HelloController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

    @RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)
    public ModelAndView hello(@PathVariable("name") String name) {

        ModelAndView model = new ModelAndView();
        model.setViewName("hello");
        model.addObject("msg", name);

        return model;

    }

}

4. Vues JSP

Une page JSP pour afficher la valeur, et inclure le bootstrap css et js.

html4strict

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



Maven + Spring MVC









${title}

Hello ${name} Welcome Welcome!

Learn more

Heading

ABC

View details

Heading

ABC

View details

Heading

ABC

View details


© Mkyong.com 2015

5. Configuration Spring XML

5.1 Enable component scanning, view resolver and resource mapping.

spring-web-servlet.xml



    

    
        
            /WEB-INF/views/jsp/
        
        
            .jsp
        
    

    

    

5.2 Declares a DispatcherServlet in web.xml. Si le fichier de configuration Spring XML n'est PAS spécifié, Spring recherchera les{servlet-name}-servlet.xml.

Dans cet exemple, Spring recherchera le fichierspring-web-servlet.xml.

web.xml



    Spring3 MVC Application

    
        spring-web
        
                    org.springframework.web.servlet.DispatcherServlet
                
        1
    

    
        spring-web
        /
    

Vous pouvez définir un fichier XML Spring viacontextConfigLocation.

web.xml

    
        spring-web
        
                     org.springframework.web.servlet.DispatcherServlet
                
        1
        
            contextConfigLocation
            /WEB-INF/spring-mvc-config.xml
        
    

        
        spring-web
        /
    

6. Demo

Le fichierpom.xml définit un conteneur Jetty intégré. Problèmemvn jetty:run pour démarrer le projet.

Terminal

$ mvn jetty:run
...
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.

spring3-mvc-maven-xml-demo

spring3-mvc-maven-xml-demo2

7. Fichier WAR

Pour créer un fichier WAR pour le déploiement:

Terminal

your-project$ mvn war:war

Un fichier WAR sera créé dans le dossierproject arget\.

${Project}\target\spring3-web-1.0-SNAPSHOT.war

Télécharger le code source

Téléchargez-le -spring3-mvc-maven-xml-hello-world.zip (51 Ko)