1

I have some input fields in my jsp page,l want to know is it possible for values passed in those fields to be passed as URL.

I tried using @RequestParam and @PathVariable on same variable to try to retrive it and put it as URI but it didnt work

This is my jsp page form:

<body>
 <form action="welcome/" method="post">
  <input type="text" name="name" value="">
  <input type="submit"  value="go">
 </form>
</body>

And this is my Controller handler method:

@PostMapping(value = "/welcome/{name}")
public String welcomeAgain(@PathVariable @RequestParam("name")String name){
    return "welcome";
}
1
  • why would you want to get them as url? if you want that you should use GET method not POST and change the controller to use @GetMapping and the requestparam should work fine
    – Yussef
    Sep 27, 2019 at 21:28

2 Answers 2

2

Just use @PathVariable("name") annotation. Exclude the @RequestParam annotation from your function.

I hope it helps.

1

To get the value passed in URL you can use @PathVariable normally with GET request.

http://localhost:8080/user/101

@RequestParam normally used with POST request for accessing the query parameter values

http://localhost:8080/user/101?param1=10&param2=20

@PostMapping(value = "/welcome/{name}")
public String welcomeAgain(@PathVariable("name") String name){
    return "welcome " + name;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.