Can you give me a brief explanation and a sample in using @PathVariable
in spring mvc? Please include on how you type the url?
I'm struggling in getting the right url to show the jsp page. Thanks.
问题:
回答1:
suppose you want to write a url to fetch some order, you can say
www.mydomain.com/order/123
where 123 is orderId.
So now the url you will use in spring mvc controller would look like
/order/{orderId}
Now order id can be declared a path variable
@RequestMapping(value = " /order/{orderId}", method=RequestMethod.GET)
public String getOrder(@PathVariable String orderId){
//fetch order
}
if you use url www.mydomain.com/order/123, then orderId variable will be populated by value 123 by spring
Also note that PathVariable differs from requestParam as pathVariable is part of URL.
The same url using request param would look like www.mydomain.com/order?orderId=123
API DOC
Spring Official Reference
回答2:
Have a look at the below code snippet.
@RequestMapping(value="/Add/{type}")
public ModelAndView addForm(@PathVariable String type ){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("addContent");
modelAndView.addObject("typelist",contentPropertyDAO.getType() );
modelAndView.addObject("property",contentPropertyDAO.get(type,0) );
return modelAndView;
}
Hope it helps in constructing your code.
回答3:
If you have url with path variables, example www.myexampl.com/item/12/update where 12 is the id and create is the variable you want to use for specifying your execution for intance in using a single form to do an update and create, you do this in your controller.
@RequestMapping(value = "/item/{id}/{method}" , RequestMethod.GET)
public String getForm(@PathVariable("id") String itemId , @PathVariable("method") String methodCall , Model model){
if(methodCall.equals("create")){
//logic
}
if(methodCall.equals("update")){
//logic
}
return "path to your form";
}
回答4:
@PathVariable used to fetch value from url
for example : To get some question
www.stackoverflow.com/questions/19803731
Here some question id
is passed as a parameter in url
Now to fetch this value in controller
all you have to do is just to pass @PathVariable and in method parameter
@RequestMapping(value = " /questions/{questionId}", method=RequestMethod.GET)
public String getQuestion(@PathVariable String questionId){
//return question details
}
回答5:
Let us assume you hit a url as www.example.com/test/111 . Now you have to retrieve value 111 (which is dynamic) to your controller method .At time you ll be using @PathVariable as follows :
@RequestMapping(value = " /test/{testvalue}", method=RequestMethod.GET)
public void test(@PathVariable String testvalue){
//you can use test value here
}
SO the variable value is retrieved from the url
回答6:
@RequestMapping(value = "/download/{documentId}", method = RequestMethod.GET)
public ModelAndView download(@PathVariable int documentId) {
ModelAndView mav = new ModelAndView();
Document document = documentService.fileDownload(documentId);
mav.setViewName("download");
mav.addObject("downloadDocument", document);
return mav;
}
@RequestMapping(value = "/download/{documentId}" == @PathVariable int documentId
回答7:
have a look at the below code snippet.
@RequestMapping(value = "edit.htm", method = RequestMethod.GET)
public ModelAndView edit(@RequestParam("id") String id) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("user", userinfoDao.findById(id));
return new ModelAndView("edit", modelMap);
}
If you want the complete project to see how it works then download it from below link:-
UserInfo Project on GitLab