Spring 3 MVC hello world example – Annotation

Technologies used :

  • Spring 3.2.13.RELEASE
  • Maven 3
  • JDK 1.6
  • Tomcat 7 or Jetty 9
  • 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 – Annotation_第1张图片
spring3-mvc-hello-world-annotation.png

P.S No more XML files like web.xml or any other Spring XML configuration files.

2. Maven

2.1 A pom.xml template to quick start a Spring MVC project. To compile this project, we need to add a servlet-api dependency.

pom.xml



    4.0.0
    com.mkyong
    spring3-mvc-maven-annotation-hello-world
    war
    1.0-SNAPSHOT
    spring mvc
    
        1.6
        3.2.13.RELEASE
        1.2
        3.1.0
    
    
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            javax.servlet
            jstl
            ${jstl.version}
        
        
        
            javax.servlet
            javax.servlet-api
            ${servletapi.version}
            provided
        
    
    
        
            
                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
                
            
        
    

2.2 To compile this project and make it supports Eclipse IDE.

Terminal

$ mvn eclipse:eclipse

3. Spring Controller

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 @JavaConfig

SpringWebConfig.java

package com.mkyong.config; 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; 

@EnableWebMvc //mvc:annotation-driven
@Configuration
@ComponentScan({ "com.mkyong.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) {   
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 
    
    @Bean 
    public InternalResourceViewResolver viewResolver() { 
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
        viewResolver.setViewClass(JstlView.class); 
        viewResolver.setPrefix("/WEB-INF/views/jsp/"); 
        viewResolver.setSuffix(".jsp"); 
        return viewResolver; 
    } 
}

XML equivalent.

 
     
     
         
            /WEB-INF/views/jsp/ 
         
         
            .jsp 
        
     
     
     

6. Servlet 3.0+ Container

Create a ServletInitializer class by extending AbstractAnnotationConfigDispatcherServletInitializer, the Servlet 3.0+ container will pick up this class and run it automatically. This is the replacement for web.xml.

MyWebInitializer.java

package com.mkyong.servlet3;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.mkyong.config.SpringWebConfig;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {  
    @Override 
    protected Class[] getServletConfigClasses() { 
        return new Class[] { 
            SpringWebConfig.class 
        }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
        return new String[] { "/" }; 
    } 

    @Override 
    protected Class[] getRootConfigClasses() { return null; }
}

7. Demo

Download the project and run it with the embedded Jetty container.
Terminal

$ mvn jetty:run

URL : http://localhost:8080/spring3

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

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

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

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