What is @pathVariable and how to use it?

0 votes
43 views
asked May 10 in JAVA by admin (8,115 points)
Hi

  What is @pathVariable and how to use it? possible provide the sample code too?

2 Answers

0 votes
answered May 10 by anonymous

@PathVariable:-
     A path variable in the URI could be injected as a parameter using the @PathVariable annotation.
For example:
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}")
public ModelAndView getEmployee(@PathVariable String id) { … }

0 votes
answered May 10 by anonymous

Review a Spring MVC @RequestMapping and @PathVariable example.

@RequestMapping("/site")
public class SiteController {
    @RequestMapping(value = "/{q}", method = RequestMethod.GET)
    public ModelAndView display(@PathVariable("q") String q) {
        logger.debug("Site : q : {}", q);
        return getModelAndView(q, "site");
    }
}
...