Spring 3 MVC hello world example

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; 
    }
}

Spring XML Configuration

spring-web-servlet.xml



  
    
   
     
      /WEB-INF/views/jsp/ 
      
     
      .jsp 
     
    
    
   


web.xml



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


    spring-web
    /

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