当前位置: 首页 > 知识库问答 >
问题:

ModelAttribute在窗体后为空

都阳辉
2023-03-14

我正试图做一个小型攀登网站作为一个个人项目,但我很难把价值放在我的ModelAttribute。

    @RequestMapping("/new_topo")
    public String addTopo(Model model){
        Topo topo = new Topo();
        Sector sector = new Sector();

        model.addAttribute("topo", topo);
        model.addAttribute("sector", sector);
        return "new_topo";
    } 

正如您所看到的,我在我的model 1中添加了Topo和Sector实例,其中数据将在此处接收:


<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Add a Spot</title>
</head>
<body>
<div th:replace="fragment/header :: header"> </div>
<h1 align="center">Add a topo</h1>
<form action="#" th:action="@{/save_topo}" method="post">
    <table align="center">
        <tr>
            <td>cotation</td>
            <td> <input type="text" th:field ="${topo.cotation}"> </td>
        </tr>
        <tr>
            <td>sector</td>
            <td> <input type="text" th:value ="${sector.name}"> </td>
        </tr>
        <tr>
            <td>spits_amount</td>
            <td> <input type="number" th:field ="${topo.spitAmount}"> </td>
        </tr>
        <tr>
            <td colspan="2"><button type="submit">Save</button> </td>
        </tr>
    </table>
</form>
</body>
</html>

问题是,当我试图访问Sector的字段时,它会说“Sector是空的”,而Topo的字段接收得非常好。我是这样检查收到的数据的:

    @RequestMapping(value = "/save_topo", method = RequestMethod.POST)
    public String saveTopo(@ModelAttribute("topo") Topo topo, @ModelAttribute("sector") Sector sector){
        System.out.println("============================");
        System.out.println(topo.getCotation() + " " + topo.getSpitAmount()); // Data 100% received
        System.out.println("============================");
        System.out.println("NAME IS EQUALS TO : " + sector.getName()); // null, did not receive the input from the form
        topo.getSector().add(sector);
        sector.setTopo(topo);
        topoService.save(topo);
        sService.save(sector);
        return "topos";
    }

请注意,我是web/后端社区的新手,我可能不知道事情是如何工作的(我认为这对您来说是显而易见的)。

感谢您的任何即将到来的帮助!

编辑:这可能有帮助?

null

共有2个答案

薄烨
2023-03-14

唯一的区别,我看到的工作和不是之间的属性字段和值。

改变

th:value ="${sector.name}"

th:field ="${sector.name}"
祁飞飙
2023-03-14

请尝试以下操作(注意name属性,它应该等于bean字段名):

 <input type="text" th:value="${sector.name}" name="name"> 

如果它不起作用,就把其余的也改成这样:

 <input type="text" th:value="${topo.cotation}" name="cotation"> </td>

 <input type="number" th:value="${topo.spitAmount}" name="spitAmount"> </td>
 类似资料:
  • 如果我错了请纠正我。两者都可以用于数据绑定。 谢谢!!

  • 我需要帮助我的代码。我已经通读了几次代码,我没有看到它有什么问题。期望用户提交作业申请并将用户重定向到仪表板,但它没有提交作业申请,也没有将用户定向到仪表板。这是我的代码:mode.py Views.py 从django.contrib.auth.decorators进口login_required 从。窗体导入addJobForm,应用程序窗体从。模型导入作业 defjob_detail(请求,

  • 它需要两种形式才能输入特定事务的所有信息。我希望能够在这两个表单之间来回翻转,保留在每个表单上输入的内容,直到‘保存’按钮被单击。 我想我应该可以使用Form2.Show,Me.Hide,然后使用Form1.Show,Me.Hide。我第一次转到Form2时,它经过了Form2 load事件(这是合理的),但对Form1上控件内容的任何知识都丢失了。即使Form1是隐藏的(而不是关闭的),其控件的

  • 我使用Angular ReactiveFormsModule创建了一个表单,但在构建应用程序时得到了这个错误消息 src/app/security/login/login.component.html中的错误:11:13-错误NG8002:无法绑定到“窗体组”,因为它不是“窗体”的已知属性。 html: login.component.ts security.module.ts

  • 主要内容:在应用程序中添加菜单和子菜单,在窗体中添加剪切,复制和粘贴功能,在窗体中锚定和停靠控件,模态窗体在本章中,我们来学习以下概念和功能应用: 在应用程序中添加菜单和子菜单 在窗体中添加剪切,复制和粘贴功能 锚定和停靠控件在窗体中 模态窗体 在应用程序中添加菜单和子菜单 传统上,,,和类用于在Windows应用程序中添加菜单,子菜单和上下文菜单。 现在,使用,,和控件替换并添加功能到以前版本的菜单相关的控件。但是,为了向后兼容和将来使用,保留了旧的控制类。 先使用旧版本控件创建一个典型的Wi