Reputation: 97
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?
Upvotes: 0
Views: 3813
Reputation: 154
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.
Upvotes: 0
Reputation: 186
@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.
Upvotes: 1