@RequestParam 注解(3)

  • 2020 年 3 月 18 日
  • 筆記

在处理方法入参使用@RequestParam 可以把请求参数传递给请求方法:

  • value:参数名
  • required:是否必须。默认为true,表示请求参数中必须包含对应的参数,若不存在,将抛出异常
// 两个参数是必须的,必须携带参数和参数值  @Controller  @RequestMapping("/springmvc")  public class HelloWorld {      @RequestMapping("/helloparam01")      public String testRequestParam(@RequestParam(value="username") String un , // 这个时候两个参数是必须的,必须携带参数和参数值                                @RequestParam(value = "age" )int age) {          System.out.println("test,username: " + un + ",age : " + age );          return "success";      }  }
// 这个时候age参数不是必须的  @Controller  @RequestMapping("/springmvc")  public class HelloWorld {      @RequestMapping("/helloparam02")      public String testRequestParam01(@RequestParam(value="username") String un , // 这个时候age参数不是必须的                                       @RequestParam(value = "age",required = false)Integer age){          System.out.println("test,username: " + un + ",age : " + age );          return "success";      }  }
// 参数使用默认值  @Controller  @RequestMapping("/springmvc")  public class HelloWorld {      @RequestMapping("/helloparam03")      public String testRequestParam02(@RequestParam(value="username") String un , // 参数使用默认值                                       @RequestParam(value = "age",required = false,defaultValue = "0")int age){          System.out.println("test,username: " + un + ",age : " + age );          return "success";      }  }