IDEAを使用した簡単なSpring MVCプロジェクトの作成(Hello World)

7796 ワード

1.原文転載
原文住所:https://www.cnblogs.com/wormday/p/8435617.html
期間中に発生した問題解決方法:1、Spring構成の問題--要素「context:component-scan」の接頭辞「context」はバインドされていません.https://blog.csdn.net/lhc1105/article/details/50456881
2.実操
実行環境:
  • Java 1.7
  • Idea 2018.3
  • Spring-4.3.18
  • Spring MVC-4.3.18

  • 2.1異なる経路の書き方
    package com.helloworld;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    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.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/hi")
    public class HelloController {
    
        /**
         *     
         * @param model
         * @return
         */
        @RequestMapping("/say")
        public String say(Model model){           //      Model
            model.addAttribute("name","wormday"); //   Model  
            model.addAttribute("url","http://www.cnblogs.com/wormday/p/8435617.html"); //   Model  
            return "say";
        }
    
        /**
         *   URL     
         *  Spring MVC ,    @PathVariable              URI      
         * @param model
         * @param params
         * @return
         */
        @RequestMapping("/pathVar/{params}")
        public String pathVar(Model model,
                              @PathVariable(value = "params") String params){
    
            model.addAttribute("params", params);
            return "pathVar";
        }
    
        /**
         *      
         *       @ResponseBody      , spring           ,      HTTP  。      ,           。
         *  :              。
         * @param model
         * @return
         */
        @RequestMapping(value = "/responseBody")
        @ResponseBody
        public String responseBody(Model model){
            return "I'm response body from back end!";
        }
    	/**
         *      
         * @param model
         * @return
         */
        @RequestMapping(value = "/redirect", method = RequestMethod.GET)
        public String redirect(Model model) {
    
            // Do somethong here
    
            return "redirect:/hi/say";
        }
    }