0

I have a form like this:

<form action="/list/${tableName}" method="post">
<p>
<select name="tableName">
<option value="employees">Employees</option>
<option value="contracts">Contracts</option>
</select>
</p>
<input type="submit" value="Submit" />
</form>

and on the controller side:

    @RequestMapping(value = "/list/{tableName}", method = { RequestMethod.POST, RequestMethod.GET })
    public String getTables(Model m, @PathVariable("tableName") String tableName) {
...
//findAll here
...
return "home";
}

It writes "not found" P.S.: what is now @PathVariable used to be @RequestParam as I'm using it in the body as well. How do I pass the options as a variable to the controller so that I get list/employees and list/contracts when I list the table data with findAll?

Nikrango
  • 87
  • 2
  • 11
  • If you need extra data or to clarify something feel free to ask! – Nikrango Apr 24 '19 at 09:06
  • I think @RequestParam should work in this scenario. And also could you please explain what do you mean by "I'm using it in the body as well"? – Jaspreet Jolly Apr 24 '19 at 09:13
  • It seems that when you submit the form, url '/list/${tableName}' does not resolve 'tableName' variable. Try to replace ${tableName} for any hardcoded value just to check that the controller is working properly. – Alexander Terekhov Apr 24 '19 at 09:16
  • Thank you for your answer! The full getTables is: ``@RequestMapping(value = "/list/{tableName}", method = { RequestMethod.POST, RequestMethod.GET }) public String getTables(Model m, @PathVariable("tableName") String tableName) { Integer idPrepend = spravochnikService.getIdAtSpravName(tableName); List spravList = spravochnikService.findAll(tableName); m.addAttribute("spravList", spravList); m.addAttribute("tableName", tableName); m.addAttribute("idPrepend", idPrepend); return "home"; }`` – Nikrango Apr 24 '19 at 09:16

2 Answers2

1

@RequestParam is use for query parameter(static values) like: http://localhost:8080/calculation/pow?base=2&ext=4

@PathVariable is use for dynamic values like : http://localhost:8080/calculation/sqrt/8

@RequestMapping(value="/pow", method=RequestMethod.GET)
public int pow(@RequestParam(value="base") int base1, @RequestParam(value="ext") int ext1){
    int pow = (int) Math.pow(base1, ext1);
    return pow;
}

@RequestMapping("/sqrt/{num}")
public double sqrt(@PathVariable(value="num") int num1){
    double sqrtnum=Math.sqrt(num1);
    return sqrtnum;
}

Here is link for more about that @PathVariable vs @RequestParam

To solve Your problem You need to see (like in debug mode) if your method findAll is actually returning some data and how You then send this data to the view.

Karol Katanowski
  • 176
  • 2
  • 13
  • Thank you! If I'm not adding the ${} and {} things it returns all I need, just the url is just /list – Nikrango Apr 24 '19 at 09:20
  • Also I would recommend to not use method = { RequestMethod.POST, RequestMethod.GET }. Just make 2 methods with annotation GetMapping and PostMapping. – Karol Katanowski Apr 24 '19 at 09:23
  • So how to make the form select option appear in the url of list? Maybe not the code, just a hint or piece of advice – Nikrango Apr 24 '19 at 09:27
  • {tableName} will be PathVariable then because it will be dynamic value depends on what You choose in form then use this value in findAll method to get list of Data from {tableName} and return it to view. – Karol Katanowski Apr 24 '19 at 09:29
  • 1
    Yes, I used it as a path variable, and if i type the uri manually, that is, /list/employees, it works, but how do I pass the tablename from the form? My jsp seems wrong – Nikrango Apr 24 '19 at 09:42
  • Jsp seems fine for me but i think You don't do anything with the variable name which is option between employees and contracts. Try using @RequestParam for name to see if it contains something. – Karol Katanowski Apr 24 '19 at 09:47
  • Yes it does contain all I need, I can use it freely if i switch my mapping to list without the variable part, I just cant make it part of the displayed uri – Nikrango Apr 24 '19 at 09:50
  • @RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST}) public String getTables(Model model, @RequestParam String tableName) { – Karol Katanowski Apr 24 '19 at 10:05
  • And for jsp

    – Karol Katanowski Apr 24 '19 at 10:05
  • But then the list will be available with just the /list uri and I'm trying to make it /list/employees lists employees and /list/contracts lists contracts data – Nikrango Apr 24 '19 at 11:31
  • then return in this method: return "redirect:/" + tableName; – Karol Katanowski Apr 24 '19 at 11:39
0

Just use the @RequestParam annotation.

As described in the documentation:

You can use the @RequestParam annotation to bind Servlet request parameters (that is, query parameters or form data) to a method argument in a controller. https://docs.spring.io/spring/docs/5.2.0.M1/spring-framework-reference/web.html#mvc-ann-requestparam

The form:

<form action="/list">
  <select name="tableName">
    <option value="employees">Employees</option>
    <option value="contracts">Contracts</option>
  </select>
  <input type="submit" value="Submit" />
</form>

The controller:

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String getTables(Model m, @RequestParam("tableName") String tableName) {
  //findAll here
  return "home";
}

If your request does not have any side-effect on the server (is read only request), good pratice is to use a GET method.

Robin Rozo
  • 154
  • 5