Spring 3 MVC hello world example

In this tutorial, we show you a Spring 3 MVC hello world example, using Maven build tool.
Technologies used :

  • Spring 3.2.13.RELEASE
  • Maven 3
  • JDK 1.6
  • Eclipse 4.4
  • Boostrap 3

1. Project Structure

Download the project source code and review the project folder structure :


Spring 3 MVC hello world example_第1张图片
spring3-mvc-maven-project.png

2. Maven

A pom.xml template to quick start a Spring MVC project, it defines Spring 3 dependencies, an embedded Jetty container and Eclipse workspace configuration.

pom.xml



    4.0.0
    com.mkyong
    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. Controller & Mapping

The @RequestMapping has been available since 2.5, but now enhanced to support REST style URLs.

HelloController.java

package com.mkyong.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. JSP Views

A JSP page to display the value, and include bootstrap css and js.

html4strict


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

${title}

Hello ${name} Welcome Welcome!

Learn more

Heading

ABC

View details

Heading

ABC

View details

Heading

ABC

View details


© Mkyong.com 2015

5. Spring XML Configuration

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. If the Spring XML configuration file is NOT specified, Spring will look for the {servlet-name}-servlet.xml.

In this example, Spring will look for the spring-web-servlet.xml file.

web.xml



    Spring3 MVC Application
    
        spring-web
        org.springframework.web.servlet.DispatcherServlet
        1
    
    
        spring-web
        /
    

You can define a Spring XML file via contextConfigLocation.

web.xml



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


    spring-web
    /

6. Demo

The pom.xml file defines an embedded Jetty container. Issues mvn jetty:run to start the project.
Terminal

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

URL : http://localhost:8080/spring3

Spring 3 MVC hello world example_第2张图片
spring3-mvc-maven-xml-demo.png

URL : http://localhost:8080/spring3/hello/mkyong

Spring 3 MVC hello world example_第3张图片
spring3-mvc-maven-xml-demo2.png

7. WAR File

To create a WAR file for deployment :
Terminal

your-project$ mvn war:war

A WAR file will be created in project\target\ folder.

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

你可能感兴趣的:(Spring 3 MVC hello world example)