Spring mvc @PathVariable

Can you give me a brief explanation and sample using @PathVariable in spring mvc? Indicate how you enter the URL? I am trying to get the correct url to show the jsp page. Thank.

+105
spring-mvc
Nov 06 '13 at 3:48
source share
9 answers

Suppose you want to write a url to get some order, you can say

 www.mydomain.com/order/123 

where 123 is orderId.

So now the URL that you will use in the spring mvc controller will look like

 /order/{orderId} 

Now the order identifier can be declared a path variable

 @RequestMapping(value = " /order/{orderId}", method=RequestMethod.GET) public String getOrder(@PathVariable String orderId){ //fetch order } 

if you use the url www.mydomain.com/order/123 then the variable orderId will be populated with 123 using spring

Also note that PathVariable is different from requestParam, since pathVariable is part of the URL. The same url using the request parameter will look like www.mydomain.com/order?orderId=123

API DOC
Spring Official Link

+198
Nov 06 '13 at 4:10
source share

Take a look at the code snippet below.

 @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 this helps in creating your code.

+9
Nov 06 '13 at 6:14
source share

If you have a URL with path variables, for example, www.myexampl.com/item/12/update, where 12 is the identifier and create is the variable that you want to use to indicate your execution to use in one form update and create, you do it 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"; } 
+5
Feb 02 '16 at 8:29
source share

@PathVariable used to get the value from the URL

for example: to get a question

 www.stackoverflow.com/questions/19803731 

Here, some question id is passed as a parameter in the URL

Now to get this value in the controller all you have to do is just pass @PathVariable in the method parameter

 @RequestMapping(value = " /questions/{questionId}", method=RequestMethod.GET) public String getQuestion(@PathVariable String questionId){ //return question details } 
+2
Nov 10 '17 at 8:58
source share

An annotation that indicates that a method parameter should be associated with a URI template variable. Supported for annotated RequestMapping 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
Apr 26 '16 at 6:55
source share

Suppose you typed the URL as www.example.com/test/111. Now you need to get the value 111 (which is dynamic) for your controller method. At the time you will use @PathVariable as follows:

 @RequestMapping(value = " /test/{testvalue}", method=RequestMethod.GET) public void test(@PathVariable String testvalue){ //you can use test value here } 

SO, the value of the variable is extracted from the URL

0
Jan 28 '16 at 7:56
source share

With Spring 4, you can use the more convenient @GetMapping annotation. @GetMapping is a compiled annotation that acts as a shortcut to @RequestMapping (method = RequestMethod.GET)

@GetMapping ("/ request / {ID}") public String getRequest (@PathVariable String id) {

0
Apr 27 '17 at 16:53 on
source share

This is one of the annotations used to display / handle dynamic URIs. You can even specify a regular expression for the dynamic parameter URI to accept only a specific type of input.

For example, if the URL to receive the book using a unique number would be:

 URL:http://localhost:8080/book/9783827319333 

The number indicated in the last URL can be obtained using @PathVariable, as shown below:

 @RequestMapping(value="/book/{ISBN}", method= RequestMethod.GET) public String showBookDetails(@PathVariable("ISBN") String id, Model model){ model.addAttribute("ISBN", id); return "bookDetails"; } 

In short, this is just another way to extract data from HTTP requests in Spring.

0
Jun 07 '19 at 3:18
source share

look 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 whole project to see how it works, download it from the bottom link: -

UserInfo Project on GitLab

-2
May 20 '15 at 20:13
source share



All Articles