美文网首页
4.Spring MVC @PathVariable注解

4.Spring MVC @PathVariable注解

作者: 765197250c12 | 来源:发表于2017-05-15 23:43 被阅读113次
    • 带占位符的URL新增的功能,该功能在SpringMVC想REST目标挺进发展过程中具有里程碑的意义*
    • 通过@PatchVariable可以将URL中的占位符参数绑定到控制器处理方法的入参中:URL 中的{xxx}占位符可以通过@PathVariable("xxx")绑定到操作方法的入参中。
    package com.gu.controller;
    
    import org.omg.CORBA.Request;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * Created by gurongkang on 17/5/8.
     */
    @Controller
    @RequestMapping(value = "/controller")
    public class HelloController {
    
      /**
       * @PathVariable可以用来映射URL的占位符到目标方法的参数中
       */
      @RequestMapping("/testPathVariable/{id}")
      public String testPathVariable(@PathVariable("id") Integer id) {
        System.out.printf("testPathVariable------>" + id);
        return "Hello";
      }
    }
    
    

    url测试

    <a href="controller/testPathVariable/10">testPathVariable</a>
    

    id 为10


    源码 https://github.com/gurongkang/TestSpringMVC.git tag 3

    相关文章

      网友评论

          本文标题:4.Spring MVC @PathVariable注解

          本文链接:https://www.haomeiwen.com/subject/ziskxxtx.html