@PathVariable与@RequestParam Dear 丶 2022-05-16 03:29 209阅读 0赞 @Request与@PathVariable注解的作用都是从前端获取传递到后台的参数值 * @PathVariable:从URI中获取参数值 * @Request:从request中获取参数值 ## 实例 ## 示例URL http://localhost:8060/studySSM/admin/admin/reset/4?userType=1 通过以下代码从URL中获取参数 @RequestMapping("/reset/{id}") public String resetPwd(..., @PathVariable(value="id") int uid, @RequestParam(value="userType", required=false) int type, ...) { System.out.println("uid:"+ uid + " userType:" + type); //打印输出 uid:4 userType:1 return ""; } 仔细阅读以上代码,就能很容易发现参数的对应关系。 ## @request的require属性 ## require属性的作用是,要求前端是否必须传入该参数值(默认true) * 前端传值 * 正常处理 * 前端未传值 * **require=false**,示例中的type参数值会被设置为null(此时就需要小心参数所使用的数值类型,如int、double等,不允许为null,会产生异常)。 * **require=true**,页面产生400错误(请求无效,要求传入userType参数)。
还没有评论,来说两句吧...