在Spring Boot中,请求参数传递通常可以使用以下几种方式:
-
不用任何注解,前后端参数一致
-
通过
@RequestParam
接收请求参数。 -
通过
@PathVariable
接收路径中的参数。 -
通过
@RequestBody
接收请求体中的JSON数据,并将其映射到Java对象。 -
通过
@ModelAttribute
接收表单提交的数据。
接一下我们使用一个项目来一一了解。
先做好准备工作,创建数据库,创建表,创建Spring Boot项目,添加依赖并配置项目信息。
数据表:
create table customer ( id int primary key, cname varchar(20) not null )character set utf8;
实体类:
import lombok.Data; @Data public class Customer { private int id; private String cname; }
一、前后端参数一致
controller的对应方法中的参数id对应前端form表单中传过来的id,controller的对应方法中的参数name对应前端form表单中传过来的name(注意数据类型的前后端兼容问题)。这样就前端可以直接传递参数到后端。如下:
前端:
<form action="/addCustomer" method="post"> <table class="table"> <caption>填写用户基本信息</caption> <tr> <td>序号:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字:</td> <td><input type="text" name="name"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交所填信息"></td> </tr> </table> </form>
后端:
@RequestMapping("/addCustomer") public String addCustomer(int id, String name, ModelMap map){ Customer customer = new Customer(); customer.setId(id); customer.setCname(name); customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
也可以直接传参给对象,对象里必须有对应字段id和cname,前端的input标签name也必须有id和name对应。如下:
前端:
<form action="/addCustomer" method="post"> <table class="table"> <caption>填写用户基本信息</caption> <tr> <td>序号:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字:</td> <td><input type="text" name="cname"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交所填信息"></td> </tr> </table> </form>
后端:
@RequestMapping("/addCustomer") public String addCustomer(Customer customer, ModelMap map){ customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
二、@RequestParam
当前端传递过来的参数名和后端controller中对应方法的参数名不一致时,例如:当前端是name,后端是cname时,可以使用注解@RequestParam指定后端参数对应的前端参数,@RequestParam写在controller对应方法参数的前面。如下:
前端:
<form action="/addCustomer" method="post"> <table class="table"> <caption>填写用户基本信息</caption> <tr> <td>序号:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字:</td> <td><input type="text" name="name"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交所填信息"></td> </tr> </table> </form>
后端:
@RequestMapping("/addCustomer") public String addCustomer(int id, @RequestParam("name") String cname, ModelMap map){ Customer customer = new Customer(); customer.setId(id); customer.setCname(cname); customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
三、@PathVariable
如果参数在URL中以/param的方式传递到后端,可以使用@PathVariable注解指定前后端参数的对应关系。如下:
假设地址栏输入:http://localhost:8080/addCustomer/10002/lisi
@RequestMapping("/addCustomer/{id}/{name}") public String addCustomer(@PathVariable("id")int id, @PathVariable("name")String name, ModelMap map){ Customer customer = new Customer(); customer.setId(id); customer.setCname(name); customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
四、@RequestBody
如果前端传递的参数是以JSON形式传递过来,可以使用@RequestBody把参数对应到后端的参数。
前端:
<form action="/addCustomer" method="post"> <table class="table"> <caption>填写用户基本信息</caption> <tr> <td>序号:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字:</td> <td><input type="text" name="name"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交所填信息"></td> </tr> </table> </form>
后端:
@RequestMapping("/addCustomer") public String addCustomer(@RequestBody int id, @RequestBody String name, ModelMap map){ Customer customer = new Customer(); customer.setId(id); customer.setCname(name); customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
当然也可以绑定到对应对象中。
@RequestMapping("/addCustomer") public String addCustomer(@RequestBody Customer customer, ModelMap map){ customerService.addCustomer(customer); map.put("customer",customer); return "show"; }
五、@ModelAttribute
ModelAttribute
用于处理表单提交和绑定表单数据到Java对象。它表示该参数应该从模型中获取。如果模型中没有该参数,Spring 会自动实例化该参数并将其添加到模型中。
前端:
<form action="/addCustomer" method="post"> <table class="table"> <caption>填写用户基本信息</caption> <tr> <td>序号:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字:</td> <td><input type="text" name="cname"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交所填信息"></td> </tr> </table> </form>
后端:
@RequestMapping("/addCustomer") public String addCustomer(@ModelAttribute Customer customer, ModelMap map){ customerService.addCustomer(customer); map.put("customer",customer); return "show"; }