【问题标题】:The controller is not catching form POST request控制器未捕获表单 POST 请求
【发布时间】:2017-09-29 11:44:24
【问题描述】:

我正在做一个简单的项目,但不知何故我无法“捕捉”对/collaborators/add 的 POST 方法调用

我找不到错误,也许很明显(我是新手)...有人可以快速看一下吗?

html 表单:

<form action="/collaborators/add" method="post">
    <h2>Manage Collaborators</h2>
    <ul class="checkbox-list">
        <li th:each="c : ${collaborators}">
            <span class="primary" th:text="${c.name}">Michael Pemulius</span>
            <div class="custom-select">
                <span class="dropdown-arrow"></span>
                <select>
                    <option value="#" selected="selected">Designer</option>
                </select>
            </div>
        </li>
    </ul>
    <div class="actions add-new-collaborator" th:object="${collaborator}">
        <input type="text" placeholder="Name..." th:field="*{name}"/>
        <div class="custom-select">
            <span class="dropdown-arrow"></span>
            <select th:field="*{role}">
                <option value="#" disabled="disabled" selected="selected">Role...</option>
                <option th:each="r : ${roles}" th:value="${r}" th:text="${r.name}">Developer</option>
            </select>
        </div>
        <button class="button" type="submit">Add</button>
    </div>
</form>

控制器:

@RequestMapping(value = "/collaborators/add", method = RequestMethod.POST)
public String addCollaborator(Model model, @Valid Collaborator collaborator, RedirectAttributes redirectAttributes, BindingResult bindingResult) {
    System.out.println("Executed");
    if (bindingResult.hasErrors()) {
        System.out.println("but with errors");
        System.out.println(bindingResult.getFieldError());
        redirectAttributes.addFlashAttribute("flashMessage", new FlashMessage("The collaborator was not created", FlashMessage.Status.FAILURE));
        redirectAttributes.addFlashAttribute("collaborator", collaborator);
        return "redirect:/collaborators";
    }
    collaboratorService.save(collaborator);
    redirectAttributes.addFlashAttribute("flashMessage", new FlashMessage("The collaborator was created", FlashMessage.Status.SUCCESS));
    return "redirect:/collaborators";
}

控制台中没有错误信息,只有 400。

我认为问题出在百里香!这里:

<div class="custom-select">
    <span class="dropdown-arrow"></span>
    <!--<select th:field="*{role}">-->
    <select>
        <option value="#" disabled="disabled" selected="selected">Role...</option>
        <!--<option th:each="r : ${roles}" th:value="${r}" th:text="${r.name}">Developer</option>-->
        <option th:each="r : ${roles}" th:value="${r}" th:text="${r.name}">Developer</option>
    </select>
</div>

我很确定是对象绑定出错。

【问题讨论】:

  • 您的表单数据保存在Model 对象中,因此您应该将模型传递给您的方法参数并从那里读取它。或者使用@ModelParameter 注释这可能是问题所在。我希望您不要像您在帖子中显示的那样尝试使用反斜杠 (\) 来调用它。错误消息也很好。见this guide
  • 试试这个,看看它是否有效:删除表单标签中的第一个/,即action="collaborators/add"而不是action="/collaborators/add"
  • 你说得对,我错过了模型(我编辑了问题)。我现在在那里,但它还没有收到请求......
  • @JackFlamp,改变了,不...
  • 尝试运行您的浏览器开发工具并检查网络选项卡。提交表单时尝试 POST 的 URL 是什么?

标签: spring forms spring-mvc controller thymeleaf


【解决方案1】:

只是为了澄清答案:

我在做:

        <form action="/collaborators/add" method="post">
            ...
            <div class="actions add-new-collaborator" th:object="${collaborator}">
               ...
            </div>
        </form>

所以我在 div 中绑定了我的对象,这是错误的。

对象必须在表单中绑定,所以不要这样做:

        <form th:action="@{/collaborators/add}" method="post" th:object="${collaborator}">
            ...
            <div class="actions add-new-collaborator" th:object="${collaborator}">
               ...
            </div>
        </form>

而且解决了!

【讨论】:

    【解决方案2】:

    你需要稍微修改一下你的html。

    <form th:action="@{/collaborators/add}" method="post" th:object="${collaborator}">
        //here you can bind all attributes inside collaborator
    

    这就是你需要做的所有改变

    【讨论】:

    • 那个很好,谢谢!那么,为什么我不能像以前那样在
      中绑定对象呢?据我了解,表单中的绑定是对一段代码的绑定和对象,该代码比应该绑定的部分大得多......为什么这不起作用? &lt;div class="actions add-new-collaborator" th:object="${collaborator}"&gt; 谢谢!
    • 因为属性对象只适用于表单,所以你可以使用内部输入,选择,复选框,..但你不能将一个对象绑定到一个div中。也许你对用 ajax 将一段代码渲染到 div 感到困惑,这是真的,但不是用于绑定对象,为此,你总是需要一个表单
    • 谢谢你的解释,我不知道我们需要在表单中绑定对象,别处。
    • 一些你现在知道的新东西 :) 你能验证响应是否有效吗?那么对其他成员也有用吗?谢谢!
    • 当然!谢谢克拉法罗。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签