前端数据到后端数据,只有部分数据传进后端,Bean中变量首字母大写问题

前端本来有四个

将他们封装到addcloseportForm中,统一传入后端,通过mapper写入数据库

前端传入后端的方法

addboxmessage() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "addboxmessage",
          this.addcloseportForm
        );
        if (res != "success") {
          return this.$message.error("操作失败");
        }
        this.$message.success("操作成功");
      });
    },

但是在后端controller接受的时候,一直都有两个是空的。

后端函数和System.out.println结果

@RequestMapping("/addboxmessage")
    public String addboxmessage(@RequestBody BoxMessage boxmessage){
        System.out.println(boxmessage);
        int i = bmdao.addboxmessage(boxmessage);
        return i >0?"success":"error";
    }

其中的boxType和LeadSeal一直显示的是null

由于认为是先输出boxmessage的,所以一开始认为是前端有问题或者前端传到后端的过程有问题

在前端调试,利用

      console.log(this.addcloseportForm.boxType);
      console.log(this.addcloseportForm.LeadSeal);

进行调试,在控制台发现可以正确输出

问题的解决

此处最大的问题是对前后端交互的流程不够了解

最终发现是在后端的Bean中未给这两个字段赋予getter、setter方法

当赋予getter和setter方法后,发现boxType可以写入数据库中了,但是LeadSeal还是Null

重新进入Bean中观察,发现由于LeadSeal的首字母是大写L,他的构造方法、getter和setter方法和别的不太一样

构造方法

public BoxMessage(String boxid, String orderid, String boxType, String leadSeal) {
        this.boxid = boxid;
        this.orderid = orderid;
        this.boxType = boxType;
        LeadSeal = leadSeal;
    }

常规的getter、setter和首字母大写的LeadSeal的方法对比

    public String getBoxType() {
        return boxType;
    }

    public void setBoxType(String boxType) {
        this.boxType = boxType;
    }

    public String getLeadSeal() {
        return LeadSeal;
    }

    public void setLeadSeal(String leadSeal) {
        LeadSeal = leadSeal;
    }

此处考虑将LeadSeal改为leadSeal试试,得到了新的构造方法和getter、setter

    public BoxMessage(String boxid, String orderid, String boxType, String leadSeal) {
        this.boxid = boxid;
        this.orderid = orderid;
        this.boxType = boxType;
        this.leadSeal = leadSeal;
    }
    public String getLeadSeal() {
        return leadSeal;
    }

    public void setLeadSeal(String leadSeal) {
        this.leadSeal = leadSeal;
    }

最终成功输出

数据库也成功写入

总结

在Java后端的Bean中,声明变量的时候,首字母不要大写,可以避免这样setter的错误。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java Spring Boot 可以通过使用模板引擎将后端传递的数据渲染到前端页面上,从而实现将后端传参给前端作为通知内容的数据。以下是一个简单的示例: 1. 首先在后端创建一个控制器,用于处理请求并传递数据: ```java @Controller public class NotificationController { @GetMapping("/notification") public String showNotification(Model model) { // 获取通知内容数据 String notificationMessage = "这是一条通知消息!"; // 将数据传递给模板引擎 model.addAttribute("notificationMessage", notificationMessage); // 返回通知页面模板 return "notification"; } } ``` 2. 在前端创建一个通知页面模板,用于显示通知内容: ```html <!DOCTYPE html> <html> <head> <title>通知页面</title> </head> <body> <h1>通知页面</h1> <p th:text="${notificationMessage}"></p> </body> </html> ``` 在这个模板,使用了 Thymeleaf 模板引擎的语法,通过 `${notificationMessage}` 获取后端传递的通知内容数据并将其渲染到页面上。 3. 最后,在应用程序启动时,需要配置 Thymeleaf 模板引擎: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ViewResolver viewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setCharacterEncoding("UTF-8"); return resolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setEnableSpringELCompiler(true); engine.setTemplateResolver(templateResolver()); return engine; } @Bean public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(); resolver.setPrefix("/WEB-INF/templates/"); resolver.setSuffix(".html"); resolver.setTemplateMode("HTML5"); resolver.setCacheable(false); return resolver; } } ``` 在这个配置,设置了模板引擎的前缀、后缀、模板模式等信息,并将其作为 ViewResolver 注入到应用程序。 通过以上步骤,就可以实现将后端传参给前端作为通知内容的数据了。当用户访问 `/notification` 路径时,将会显示通知页面,并将后端传递的通知内容数据渲染到页面上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值