[英]Get last part of URL path
I'm trying to get the last string after user with this request handler. 我正在尝试使用此请求处理程序获取用户之后的最后一个字符串。 Is it possible to do this without being hacky? 是否有可能做到这一点而又不被束缚?
@Controller
@RequestMapping("/user/*")
public class Student{
@RequestMapping(method = RequestMethod.GET)
public String getUser(ModelMap model, /*, parameter that gets user id */) {
// function that gets user id
model.addAttribute("foo", "foo");
return "bar";
}
}
Spring has @PathVariable for the same. Spring具有@PathVariable 。
@Controller
public class Student{
@RequestMapping("/user/{id}")
public String getUser(ModelMap model, @PathVariable String id) {
// function that gets user id
model.addAttribute("foo", "foo");
return "bar";
}
}
Here I have assumed that user id is of String type, you may change the type of id
as per your need. 在这里,我假设用户ID是String类型,您可以根据需要更改id
类型。
Try to use @RequestParam
like this: 尝试像这样使用@RequestParam
:
@Controller
public class Student{
@RequestMapping(value="/user/{user_id}", method = RequestMethod.GET)
public String getUser(ModelMap model, @RequestParam(value="user_id") Long user_id){
// function that gets user id
model.addAttribute("foo", "foo");
return "bar";
}
}
Or use @PathVariable
: 或使用@PathVariable
:
@Controller
public class Student{
@RequestMapping(value="/user/{user_id}", method = RequestMethod.GET)
public String getUser(ModelMap model, @PathVariable("user_id") Long user_id){
// function that gets user id
model.addAttribute("foo", "foo");
return "bar";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.