4、Controller的使用


  1. @Controller
    @ResponseBody
    1. 等同于RESTController
  2. RequestMapping的使用
    1. 给同一个方法指定多个RequestMapping
    2. 给类添加RequestMapping
  3. 指定RequestMapping的()method属性

  4. 如何处理URL里面的参数?
    1. @PathVariable的使用:
      1. http://127.0.0.1:8080/hello/say/243
      2. http://127.0.0.1:8080/hello/243/say
      3. package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
        
        //    @Value("${cupSize}")
        //    private String cupSize;
        //
        //    @Value("${age}")
        //    private  Integer age;
        //
        //    @Value("${content}")
        //    private String content;
        
            @Autowired
            private GirlProperties girlProperties;
        
            @RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET)
            public String say(@PathVariable("id") Integer id){
        //        return girlProperties.getCupSize();
                return "id: "+id;
            }
        
        }
        //把id写在了say的后面
        package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
        
        //    @Value("${cupSize}")
        //    private String cupSize;
        //
        //    @Value("${age}")
        //    private  Integer age;
        //
        //    @Value("${content}")
        //    private String content;
        
            @Autowired
            private GirlProperties girlProperties;
        
            @RequestMapping(value = {"/{id}/say"},method = RequestMethod.GET)
            public String say(@PathVariable("id") Integer id){
        //        return girlProperties.getCupSize();
                return "id: "+id;
            }
        
        }
        //把id写在了say的前面
    2. @RequestParam的使用方式
      1. http://127.0.0.1:8080/hello/say?id=22223
      2. package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
        
        //    @Value("${cupSize}")
        //    private String cupSize;
        //
        //    @Value("${age}")
        //    private  Integer age;
        //
        //    @Value("${content}")
        //    private String content;
        
            @Autowired
            private GirlProperties girlProperties;
        
            @RequestMapping(value = {"/say"},method = RequestMethod.GET)
            public String say(@RequestParam("id") Integer id){
        //        return girlProperties.getCupSize();
                return "id: "+id;
            }
        
        }
        
        package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
        
            @Autowired
            private GirlProperties girlProperties;
        
            @RequestMapping(value = {"/say"},method = RequestMethod.GET)
            public String say(@RequestParam("id") Integer myId){
        //        return girlProperties.getCupSize();
                return "id: "+myId;
            }
        
        }
        
  5. 给url当中的参数设置默认值
    1. package com.girl;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.*;
      
      /**
       * Created by sunnyangzs on 2018/3/25.
       */
      
      @RestController
      @RequestMapping("/hello")
      public class HelloController {
      
          @Autowired
          private GirlProperties girlProperties;
      
          @RequestMapping(value = {"/say"},method = RequestMethod.GET)
          public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
      //        return girlProperties.getCupSize();
              return "id: "+myId;
          }
      
      }
      
  6. 组合注解的使用
    1. GetMapping
      1. package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
        
            @Autowired
            private GirlProperties girlProperties;
        
            //@RequestMapping(value = {"/say"},method = RequestMethod.GET)
            @GetMapping(value="/say")
            public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
        //        return girlProperties.getCupSize();
                return "id: "+myId;
            }
        
        }
        




posted @ 2018-03-26 08:20  sunnyangzs  阅读(182)  评论(0编辑  收藏  举报