Spring Boot请求参数传递与接收

在Spring Boot中,请求参数传递通常可以使用以下几种方式:

  1. 不用任何注解,前后端参数一致

  2. 通过@RequestParam接收请求参数。

  3. 通过@PathVariable接收路径中的参数。

  4. 通过@RequestBody接收请求体中的JSON数据,并将其映射到Java对象。

  5. 通过@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";
}
  • 21
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值