@RequestMapping(参数)
参数path ,value 都是路径,可以存在多个路径,当在浏览器中输入相应路径都会对应到该Mapping下的函数中,进行处理
如输入localhost:8080/profile,localhost:8080/name,localhost:8080/p
@RequestMapping(path = {"/home", "/test1"})//路径 路径别名(其实就是两个路径对应同一个东西)
@ResponseBody//返回字符串而不是模板
public String home1() {
return "testhome1";
}
参数param,可以指定输入参数为固定的值时由该函数处理
@RequestMapping(path = {"/fetch"}, params = {
"personId=10"
},method = RequestMethod.GET)//
@ResponseBody
public String getParams(@RequestParam("personId") int id) {
return "Fetched parameter using params attribute = " +id;
}
@RequestMapping(value = "/fetch", params = {
"personId=20"
},method = RequestMethod.GET)
@ResponseBody
public String getParamsDifferent(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
@ResponseBody
如果有的话就会返回字符串(return 字符串),如果没有的话就会返回字符串对应的模板(html),如果有两个相同路径的函数,优先返回模板
@PathVariable
传递的是路径里面的参数如下面的userId,相当于给param = userId(自己写的)
@RequestMapping(path = {"/profile/{userId}"})//{}代表路径里面的参数 value={} 和path一个效果
@ResponseBody//返回字符串而不是模板
public String home2(@PathVariable("userId") String param) {//@ 取了路径中的参数,用别名代替
return "testhome1" + "用户:" + param + "的";
}
@RequestParam
路径中提交的参数,如@RequestMapping(path = {"/like"})
如:此时在浏览器中输入localhost:8080/like?user=aa&id=10&countryId=20
@RequestMapping(value = "/like",params = {
"countryId=20"
},method = RequestMethod.GET)
@ResponseBody
public String like(@RequestParam("user") String User,@RequestParam("id") String Id,@RequestParam("countryId") String country) {
return "用户名:"+User+"<br/>"+"id号:"+Id+"<br/>"+"国家:"+country;
}
注意:
@RequestParam中的参数是@RequestMapping中的path在浏览器中输入后 通过?连接,多个参数使用&连接中的值,@RequestMapping中不包含或者通过param参数指定特定输入值
@PathVariable是@RequestMapping中的path中的内容,在@RequestMapping中以/连接{}包装,也就是 @RequestMapping中包含
小白刚刚开始摸索,如有错误请不吝指出,非常感谢