Spring 控制器未捕获表单POST请求
我正在做一个简单的项目,不知何故,我无法“捕获”对Spring 控制器未捕获表单POST请求,spring,forms,spring-mvc,controller,thymeleaf,Spring,Forms,Spring Mvc,Controller,Thymeleaf,我正在做一个简单的项目,不知何故,我无法“捕获”对/collaborators/add的POST方法调用 我找不到错误,也许很明显(我是个新手)…有人能看一下吗 html表单: <form action="/collaborators/add" method="post"> <h2>Manage Collaborators</h2> <ul class="checkbox-list"> <li th:each=
/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>
角色
开发商
我敢肯定对象绑定有错误。您需要稍微修改一下html
<form th:action="@{/collaborators/add}" method="post" th:object="${collaborator}">
//here you can bind all attributes inside collaborator
//在这里,您可以绑定collaborator内部的所有属性
这就是您需要做的所有更改,基本上您需要稍微修改一下html
<form th:action="@{/collaborators/add}" method="post" th:object="${collaborator}">
//here you can bind all attributes inside collaborator
//在这里,您可以绑定collaborator内部的所有属性
这就是你需要做的所有改变,基本上就是为了澄清答案:
我在做:
<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>
...
...
问题解决了 为了澄清答案:
我在做:
<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>
...
...
问题解决了 表单数据保存在Model
对象中,因此您应该将模型传递给方法params并从那里读取它。或者使用@ModelParameter
注释,这可能就是问题所在。我希望你不要像在你的帖子中那样用反斜杠(\)来称呼它。错误消息也会很好。请参见“如果有效,请尝试此操作和se”:删除表单中的第一个/
标记,即action=“collaborators/add”
而不是action=“/collaborators/add”
您是对的,我错过了模型(并且编辑了问题)。我现在在那里,但它还没有捕获请求…@JackFlamp,已更改,没有…尝试运行浏览器开发工具并检查网络选项卡。提交表单时,试图发布到哪个URL?表单数据保存在Model
对象中,因此应将模型传递给方法参数并从中读取。或者使用@ModelParameter
注释,这可能就是问题所在。我希望你不要像在你的帖子中那样用反斜杠(\)来称呼它。错误消息也会很好。请参见“如果有效,请尝试此操作和se”:删除表单中的第一个/
标记,即action=“collaborators/add”
而不是action=“/collaborators/add”
您是对的,我错过了模型(并且编辑了问题)。我现在在那里,但它还没有捕获请求…@JackFlamp,已更改,没有…尝试运行浏览器开发工具并检查网络选项卡。当你提交表单时,你想发布到哪个URL?那个很好,谢谢!那么,为什么我不能像刚才那样在中绑定对象呢?据我所知,表单中的绑定是绑定和对象到一段比应该绑定的代码大得多的代码。。。为什么这不起作用<代码>
谢谢!因为属性对象只适用于表单,所以您可以使用内部输入、选择、复选框……但您不能将对象绑定到div中。可能您混淆了使用ajax将一段代码渲染到div中,这是正确的,但不适用于绑定对象,为此,您需要一个表单感谢您的解释,我不知道我们需要在表单中绑定对象,而不是在其他地方。您现在知道的一些新内容:)您能验证响应是否有效吗?那么对其他成员也有用吗?谢谢当然谢谢你,克拉法罗。那一个很好,谢谢!那么,为什么我不能像刚才那样在中绑定对象呢?据我所知,表单中的绑定是绑定和对象到一段比应该绑定的代码大得多的代码。。。为什么这不起作用<代码>谢谢!因为属性对象只适用于表单,所以您可以使用内部输入、选择、复选框……但您不能将对象绑定到div中。可能您混淆了使用ajax将一段代码渲染到div中,这是正确的,但不适用于绑定对象,为此,您需要一个表单感谢您的解释,我不知道我们需要在表单中绑定对象,而不是在其他地方。您现在知道的一些新内容:)您能验证响应是否有效吗?那么对其他成员也有用吗?谢谢当然谢谢你,克拉法罗。