zoukankan      html  css  js  c++  java
  • 转载:浅析@PathVariable 和 @RequestParam

    在网上看了一篇很好的文章,讲的很清楚明了,说到了点子上(转自:https://blog.csdn.net/chuck_kui/article/details/55506723):

    首先 上两个地址:

    地址① http://localhost:8989/SSSP/emps?pageNo=2

    地址② http://localhost:8989/SSSP/emp/7  (7这个参数叫“动态参数”)

    如果想获取地址①中的 pageNo的值 ‘2’ ,则使用  @RequestParam,

    如果想获取地址②中的 emp/7 中的 ‘7 ’   则使用 @PathVariable


    获取地址① 中的‘2’ 使用的 方法是如下:

     1 @RequestMapping("/emps")  
     2 public String list(@RequestParam(value="pageNo",required=false,  
     3         defaultValue="1")String pageNoStr,Map<String, Object>map){  
     4       
     5     int pageNo = 1;     
     6     try {  
     7         //对pageNo 的校验   
     8         pageNo = Integer.parseInt(pageNoStr);  
     9         if(pageNo<1){  
    10             pageNo = 1;  
    11         }  
    12     } catch (Exception e) {
    13     }  
    14     Page<Employee> page = employeeService.getPage(pageNo, 5);  
    15     map.put("page",page);  
    16       
    17     return "emp/list";  
    18 }  
    View Code

    获取地址② 中的 ‘7’ 使用的方法是如下:

    1 @RequestMapping(value="/emp/{id}",method=RequestMethod.GET)  
    2 public String edit(@PathVariable("id")Integer id,Map<String , Object>map){  
    3     Employee employee = employeeService.getEmployee(id);  
    4     List<Department> departments = departmentService.getAll();  
    5     map.put("employee", employee);  
    6     map.put("departments", departments);  
    7     return "emp/input";  
    8 } 
    View Code

    大道理不讲 原理也不分析就记忆一点,那一点呢? 看‘这个符号‘?’ 

    1. 若获取的入参的 参数 是下面这种形式 就使用 @requestParam 去获取 参数‘2’

    /emps?pageNo=2

    2. 若获取的入参的 参数 是下面这种形式 就使用 @PathVariable 去获取参数 ‘7’

    /emp/7

    @PathVariable是用来获得请求url中的动态参数


    理论 可看 下面的博文

    http://blog.csdn.net/walkerjong/article/details/7946109   

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    http://dorole.com/tag/uri-template/

    http://blog.csdn.net/jaryle/article/details/51851120     

     @pathvariable和@RequestParam注解的区别

    使用动态参数的例子:

    页面:

    1 function login() {
    2     var url = "${pageContext.request.contextPath}/person/login/";
    3     var query = $('#id').val() + '/' + $('#name').val() + '/' + $('#status').val();
    4     url += query;
    5     $.get(url, function(data) {
    6         alert("id: " + data.id + "name: " + data.name + "status: "
    7                 + data.status);
    8     });
    9 }
    View Code

    后台:

     1 /**
     2 * @RequestMapping(value = "user/login/{id}/{name}/{status}") 中的 {id}/{name}/{status}
     3 * 与 @PathVariable int id、@PathVariable String name、@PathVariable boolean status
     4 * 一一对应,按名匹配。
     5 */
     6 
     7 @RequestMapping(value = "user/login/{id}/{name}/{status}")
     8 @ResponseBody
     9 //@PathVariable注解下的数据类型均可用
    10 public User login(@PathVariable int id, @PathVariable String name, @PathVariable boolean status) {
    11 //返回一个User对象响应ajax的请求
    12     return new User(id, name, status);
    13 }
    View Code
  • 相关阅读:
    Integration Services 学习(4):包配置
    Integration Services 学习(3)
    AsyncTask的使用
    asp.net下实现支持文件分块多点异步上传的 Web Services
    wcf/web service 编码
    c# 接发邮件2
    Integration Services 学习
    使用AChartEngine画柱状图
    Cocos2d开发系列(五)
    灵活使用XMultipleSeriesRenderer设置自定义的轴标签
  • 原文地址:https://www.cnblogs.com/mySummer/p/9073117.html
Copyright © 2011-2022 走看看