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";
}
Djordje Vuckovic
  • 383
  • 1
  • 5
  • 11
  • 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 '19 at 21:28

2 Answers2

2

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

I hope it helps.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
angelboxes
  • 21
  • 3
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;
}
Romil Patel
  • 12,879
  • 7
  • 47
  • 76