I'm using spring mvc with thymeleaf to save a form with a dropdown value. The problem is when saving, I get the error 400 bad request only when I append the foreign key field in the form as a dropdown with th:field="studentTypeCode"
.
我正在使用带有百万美元的spring mvc来保存一个带有下拉值的表单。问题是在保存时,只有当我将表单中的外键字段附加为th:field =“studentTypeCode”的下拉列表时,才会收到错误400错误请求。
<select id="typeSelect" class="text_box" th:field="*{studentTypeCode}">
<div th:each="studentTypeCode : ${studentTypeCodes}" th:remove="tag">
<option th:value="${studentTypeCode.typeId}" th:text="${studentTypeCode.typeName}" />
</div>
</select>
Student.java
public class Student{
private String name;
//... other fields
//..
StudentTypeCode studentTypeCode;
//getters and setters
}
And in the controller I get the Student object using the th:object
with the
在控制器中,我使用th:对象获取Student对象
@ModelAttribute Student student
param. Saving the form throws me 400 bad request since the field studentTypeCode
is not correctly sent in the request.
@ModelAttribute学生学生座位。由于字段studentTypeCode未在请求中正确发送,因此保存表单会引发400个错误请求。
1 个解决方案
#1
0
I just found that since I'm using the @ModelAttribute
I just need to send the proper value to the Student object with the student type code id. Declaring th:field="*{studentTypeCode.studentTypeId}"
gives me the exact value(int) selected in the dropdown and I can save this value.
我刚刚发现因为我正在使用@ModelAttribute,所以我只需要使用学生类型代码id将正确的值发送到Student对象。声明:field =“* {studentTypeCode.studentTypeId}”给出了下拉列表中选中的确切值(int),我可以保存此值。
The following code,
以下代码,
<select id="typeSelect" class="text_box" th:field="*{studentTypeCode.studentTypeId}">
<div th:each="studentTypeCode : ${studentTypeCodes}" th:remove="tag">
<option th:value="${studentTypeCode.typeId}" th:text="${studentTypeCode.typeName}" />
</div>
</select>
solved my issue. Also add the BindingResult error
after your @ModelAttribute field
in the @RequestMapping
. Read more this article.
解决了我的问题。还要在@RequestMapping中的@ModelAttribute字段后添加BindingResult错误。阅读更多这篇文章。
#1
0
I just found that since I'm using the @ModelAttribute
I just need to send the proper value to the Student object with the student type code id. Declaring th:field="*{studentTypeCode.studentTypeId}"
gives me the exact value(int) selected in the dropdown and I can save this value.
我刚刚发现因为我正在使用@ModelAttribute,所以我只需要使用学生类型代码id将正确的值发送到Student对象。声明:field =“* {studentTypeCode.studentTypeId}”给出了下拉列表中选中的确切值(int),我可以保存此值。
The following code,
以下代码,
<select id="typeSelect" class="text_box" th:field="*{studentTypeCode.studentTypeId}">
<div th:each="studentTypeCode : ${studentTypeCodes}" th:remove="tag">
<option th:value="${studentTypeCode.typeId}" th:text="${studentTypeCode.typeName}" />
</div>
</select>
solved my issue. Also add the BindingResult error
after your @ModelAttribute field
in the @RequestMapping
. Read more this article.
解决了我的问题。还要在@RequestMapping中的@ModelAttribute字段后添加BindingResult错误。阅读更多这篇文章。