SpringMVC后端获取前端数据的方式(数据注入参数)

前端form表单等方式提交数据,后端如何接收数据?采用数据注入的方式,将前端提交的数据注入到方法的参数中。

一.单个数据注入:

  • 1.前端:
<form method="post"  action="${pageContext.request.contextPath}/req.action">
    <label>姓名:</label><input type="text" name="username">
    <label>年龄:</label><input type="text" name="age">
    <input type="submit" value="提交">
</form>
  • 2.后端:
package com.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataSubmitAction {
    @RequestMapping("/req.action")
    public String one(String username,int age){
        System.out.println(username+" "+age);
        return "main";
    }
}
  • 3.执行结果:
    在这里插入图片描述
  • 4.说明:
    在方法中声明参数username,age,框架会按照参数名称寻找表单中name属性的属性值为username,age的标签,并自动将标签中的value值注入到方法的参数中。

二、对象封装注入:

  • 1.前端:
<form method="post"  action="${pageContext.request.contextPath}/req.action">
    <label>姓名:</label><input type="text" name="username">
    <label>年龄:</label><input type="text" name="age">
    <input type="submit" value="提交">
</form>
  • 2.后端:
package com.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataSubmitAction {
    @RequestMapping("/req.action")
    public String one(User user){
        System.out.println(user);
        return "main";
    }
}
//自定义的实体类
class User{
    private String username;//必须和前端name属性名一致
    private int age;//必须和前端name属性名一致

    public User() {
    }

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    //必须要有set、get方法,因为前端数据向后端注入时底层调用set、get方法
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username=" + username +
                ", age=" + age +
                '}';
    }
}
  • 3.执行结果:
    在这里插入图片描述

  • 4.说明:
    在方法中声明一个自定义的实体类参数,框架调用实体类中相应的setter方法注入属性值,只要保证实体类中成员变量的名称与提交请求的name属性值一致即可。
    在提交请求中,保证请求参数的名称与实体类中成员变量的名称一致,则可以自动创建对象,则可以自动提交数据,自动类型转换,自动封装数据到对象中。

三、@PathVariable动态占位符注入:

(仅用于超链接)

  • 1.前端:
<a href="${pageContext.request.contextPath}/req/张三/19.action">超链接</a>
  • 2.后端:
package com.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataSubmitAction {
    @RequestMapping("/req/{username}/{age}.action")
    public String one(
            @PathVariable("username")
            String username,
            @PathVariable("age")
            int age){
        System.out.println(username+" "+age);
        return "main";
    }
}
  • 3.执行结果:
    在这里插入图片描述

  • 4.说明:
    前端在URI:req.action之间通过 /值 的方式键入值,后端通过 /{} 接收值,并通过 @PathVariable 注解将前端传来的数据注入到参数中。

四、 @RequestParam请求参数名称与形参名称不一致时:

  • 1.前端:
<form method="post"  action="${pageContext.request.contextPath}/req.action">
    <label>姓名:</label><input type="text" name="username">
    <label>年龄:</label><input type="text" name="age">
    <input type="submit" value="提交">
</form>
  • 2.后端:
package com.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DataSubmitAction {
    @RequestMapping("/req.action")
    public String one(
            @RequestParam("username")
            String uname,
            @RequestParam("age")
            int uage){
        System.out.println(uname+" "+uage);
        return "main";
    }
}
  • 3.执行结果:
    在这里插入图片描述

  • 4.说明:
    使用 @RequestParam 注解将前端传来的数据注入到参数中。

五、HttpServletRequest手动注入:

  • 1.前端:
<form method="post"  action="${pageContext.request.contextPath}/req.action">
    <label>姓名:</label><input type="text" name="username">
    <label>年龄:</label><input type="text" name="age">
    <input type="submit" value="提交">
</form>
  • 2.后端:
package com.user.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataSubmitAction {
    @RequestMapping("/req.action")
    public String one(HttpServletRequest req){
        String username = req.getParameter("username");
        String age = req.getParameter("age");//方法返回值是String类型
        System.out.println(username+" "+age);
        return "main";
    }
}
  • 3.执行结果:
    在这里插入图片描述

  • 4.说明:
    javaweb原始获取数据的方式,在方法参数中声明一个request对象,该request对象就是发送http请求的对象,使用request的getParameter()获取前端提交的数据,但是这样得到的数据需要手动进行数据类型的转换。

六、Date类型数据注入:

点击查看日期注入文章。

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姓蔡小朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值