Spring MVC hello world annotation example

Technologies used :

  • Spring 2.5.6
  • JDK 1.6
  • Maven 3
  • Eclipse 3.6

1. Directory Structure

Spring MVC hello world annotation example_第1张图片
spring2-mvc-annotation-hello-world.png

2. Maven

Spring’s annotation is bundled in the same ·spring-webmvc.jar·.

pom.xml



    4.0.0
    com.mkyong.common
    spring2-mvc-annotation-hello-world
    war
    1.0-SNAPSHOT
    Spring 2 MVC
    
        1.6
        2.5.6
        1.2
        2.5
    
    
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
        
            javax.servlet
            jstl
            ${jstl.version}
        
        
        
            javax.servlet
            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
                    
                        /spring2
                    
                
            
            
                org.apache.maven.plugins
                maven-eclipse-plugin
                2.9
                
                    true
                    true
                    2.0
                    spring2
                
            
        
    

3. Controller & Handler Mapping

Now, you can use @Controller and @RequestMapping to replace the XML configuration.

  • Controller – The controller class is no longer need to extend the base controller like AbstractController or SimpleFormController, just simply annotate the class with a @Controller annotation.
  • Handler Mapping – No more declaration for the handler mapping like BeanNameUrlHandlerMapping, ControllerClassNameHandlerMapping or SimpleUrlHandlerMapping, all are replaced with a standard @RequestMapping annotation.

HelloWorldController.java

package com.mkyong.common.controller;
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
@RequestMapping("/welcome")
public class HelloWorldController{  
    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView helloWorld(){       
        ModelAndView model = new ModelAndView("HelloWorldPage");        
        model.addObject("msg", "hello world");      
        return model;   
    }
}

If the @RequestMapping is applied at the class level (can apply at method level with multi-actions controller), it required to put a RequestMethod to indicate which method to handle the mapping request.
In this case, if a URI pattern /welcome is requested, it will map to this HelloWorldController
, and handle the request with helloWorld() method.

4. Spring XML Configuration

You still need to configure the view resolver and component scanning in XML file.

/WEB-INF/spring-mvc-config.xml

   
          
                    
            /WEB-INF/pages/      
             
                
           .jsp      
         
         
    

5. JSP Page

A simple JSP page for demonstration.

HelloWorldPage.jsp.

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

      
        

Spring MVC Hello World Annotation Example

${msg}

6. web.xml

web.xml



    Spring Web MVC Application
    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/spring-mvc-config.xml
        
        1
    
    
        mvc-dispatcher
        *.htm
    

7. Demo

7.1 To run with the embedded Jetty, type :

$ mvn jetty:run

URL : http://localhost:8080/spring2/welcome.htm

Spring MVC hello world annotation example_第2张图片
spring2-mvc-annotation-demo.png

7.2 To import into Eclipse IDE.

$ mvn eclipse:eclipse

Note
If you compare this Spring MVC annotation-based hello world example with XML, you can see that this annotation approach is easier and flexible in wiring the controller class and URL handler mapping, because you do not need to declare the controller class explicitly or extends any particular class.

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