美文网首页
@RequestParam 注解

@RequestParam 注解

作者: 桑鱼nicoo | 来源:发表于2020-02-09 20:52 被阅读0次

在处理方法入参使用@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";
    }
}

相关文章

网友评论

      本文标题:@RequestParam 注解

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