ChatGPT解决这个技术问题 Extra ChatGPT

Spring mvc @PathVariable

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.

showing 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

C
Community

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


in using pathVariable. do i need to import something? or any dependency? thanks.
you just need to import PathVariable annotation import org.springframework.web.bind.annotation.PathVariable
yeah provide some details, what you trying to do and whats the issue
I want to have www.mydomain.com/order/abc.def, now in controller I am getting only abc
A
Ahmed Ashour

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.


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. :)
I think you need to write @PathVariable("type") to tie to the path.
I want to have www.mydomain.com/order/abc.def, now in controller I am getting only abc
got it used regexp [a-zA-z0-9.]
o
ochiWlad

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";
    }

This mechanism ensure that a path variable is tied to a given variable.
A
Ajinkya

@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
}

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?
0
0gam

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;
}

G
Gagan

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


A
Arahan Jha

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.


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?
S
Saurabh Juneja

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