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.

- 5,548
- 10
- 52
- 88

- 1,835
- 3
- 16
- 22
-
2showing a jsp through spring mvc controller is done through view or say `ModelAndView`. `@PathVariable` annotation used to get variable name and its value at controller end. e.g. www.abcd.com/api/value=34455&anotherValue=skjdfjhks here **value** and **anotherValue** is variable which you can get using @PathVariable("value") int value and @PathVariable("anotherValue")String anotherValue – Aman Gupta Nov 06 '13 at 04:13
8 Answers
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
-
in using pathVariable. do i need to import something? or any dependency? thanks. – james Nov 06 '13 at 04:17
-
you just need to import PathVariable annotation import org.springframework.web.bind.annotation.PathVariable – coder Nov 06 '13 at 04:18
-
-
http://stackoverflow.com/questions/19804202/pathvariable-location-spring-mvc.. here is the code – james Nov 06 '13 at 04:41
-
I want to have www.mydomain.com/order/abc.def, now in controller I am getting only abc – prem30488 Nov 03 '14 at 08:56
-
You can use @PathVariable int orderId, spring will manage the conversion automatically if the value passed is valid integer. – coder Nov 21 '15 at 05:30
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.

- 5,179
- 10
- 35
- 56

- 161
- 1
- 2
-
im not familiar with model and view. hehe. thanks btw.. looking forward on implementing this on my project.. but i need to finish it first. :) – james Nov 06 '13 at 06:21
-
-
1I want to have www.mydomain.com/order/abc.def, now in controller I am getting only abc – prem30488 Nov 03 '14 at 08:56
-
1
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 instance in using a single form to do an update and create, you do this in your controller.
@PostMapping(value = "/item/{id}/{method}")
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,242
- 2
- 13
- 17
-
This mechanism ensure that a path variable is tied to a given variable. – ochiWlad Feb 02 '16 at 08:31
@PathVariable
used to fetch the 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 in the method parameter
@RequestMapping(value = " /questions/{questionId}", method=RequestMethod.GET)
public String getQuestion(@PathVariable String questionId){
//return question details
}

- 22,324
- 33
- 110
- 161

- 23
- 6
-
How does the form for this look like? Can I use one of the input values to specify the {ISBN} value? If yes, how do I construct the full URL for the form parameter th:action? – Jan Horčička Apr 29 '20 at 20:31
Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods.
@RequestMapping(value = "/download/{documentId}", method = RequestMethod.GET)
public ModelAndView download(@PathVariable int documentId) {
ModelAndView mav = new ModelAndView();
Document document = documentService.fileDownload(documentId);
mav.addObject("downloadDocument", document);
mav.setViewName("download");
return mav;
}

- 1,343
- 1
- 9
- 21
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

- 11
- 2
It is one of the annotation used to map/handle dynamic URIs. You can even specify a regular expression for URI dynamic parameter to accept only specific type of input.
For example, if the URL to retrieve a book using a unique number would be:
URL:http://localhost:8080/book/9783827319333
The number denoted at the last of the URL can be fetched using @PathVariable as shown:
@RequestMapping(value="/book/{ISBN}", method= RequestMethod.GET)
public String showBookDetails(@PathVariable("ISBN") String id,
Model model){
model.addAttribute("ISBN", id);
return "bookDetails";
}
In short it is just another was to extract data from HTTP requests in Spring.

- 13
- 1
- 8
-
How does the form for this look like? Can I use one of the input values to specify the {ISBN} value? If yes, how do I construct the full URL for the form parameter th:action? – Jan Horčička Apr 29 '20 at 20:31
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:-

- 1,187
- 1
- 8
- 12