zoukankan      html  css  js  c++  java
  • spring mvc-@RequestParam

    https://www.cnblogs.com/caoyc/p/5635427.html

    案例说明:

    @RequestMapping("user/add")
    
    public String add(@RequestParam("name") String name,@RequestParam("age") int age){
    
      System.out.println(name +","+age);
    
      return "hello";
    
    }

    测试1:

    当我们请求路径为:http://localhost:8080/springmvc-1/user/add?name=zhangsan&age=18

    输出结果:zhangsan,18

    测试2:

    当我们请求路径为:http://localhost:8080/springmvc-1/user/add?age=18

    输出结果:有异常出现。意思是说必须要有该参数。

    解决方案:在@RequestParam标签中添加一个required=false,表示该属性不是必须的:

    @RequestParam(value="name",required=false)

    输出结果:null,18

    测试3:

    当我们请求路径为:http://localhost:8080/springmvc-1/user/add?name=zhangsan

    同样出现上面的异常:

    那么根据上面的方法设置:

    @RequestParam(value="age",required=false) int age

    结果再运行。还是抛出异常:

    这里也说的很明白,大概意思是说不能将一个null的空置赋给age。应该使用包装类。

    改为这样:

    @RequestParam(value="age",required=false) Integer age

    结果正确输出:zhangsan,null

    还有另外一种改法:给参数指定一个默认值

    @RequestParam(value="age",required=false,defaultValue="0") int age

    结果输出:zhangsan,0

    总结:对应@RequestParam基本类型的参数我们最好都使用包装类型。

    还有相似的注解:

    @RequestHeader,使用方式和@RequestParam一样。

  • 相关阅读:
    Python print() 函数
    Python issubclass() 函数
    Python execfile() 函数
    Python basestring() 函数
    QTP自动化测试-点滴-步骤
    qtp自动化测试-条件语句 if select case
    学习心态--笔记
    测试计划小记
    QTP自动化测试-笔记 注释、大小写
    win10 新建文件夹没有了
  • 原文地址:https://www.cnblogs.com/arrows/p/10523418.html
Copyright © 2011-2022 走看看