我在尝试从帖子中获取值时遇到问题:
这是我的 HTML
<td>
<form class="form-inline" action="/infopedidos" th:action="@{/infopedidos}" th:object="${newRider}" method="post">
<select>
<option th:each="riders: ${TodosLosRiders}" th:value="${riders.getRider_id()}" th:text="${riders.getRemail()}">
</option>
</select>
<button type="submit" value="Submit" class="btn btn-primary">Asignar rider</button>
</form>
</td>
我正在尝试在帖子中获取值“riders.getRider_id()”
这是我的 Controller :
@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value="$riders.getRider_id()}") String param) throws ParseException {
我遇到的错误是下一个:
Required String parameter 'riders.getRider_id()' is not present
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'riders.getRider_id()' is not present
你们能帮帮我吗?谢谢!
最佳答案
我建议您阅读此内容:https://www.baeldung.com/thymeleaf-in-spring-mvc
您的表单当前仅发送您的“newRider”对象作为@ModelAttribute。但您的选择标签未指向对象中的任何字段。这意味着您不会更改表单输入中的对象。只能发送了。如果您想编辑“newRider”对象并通过帖子发送它,您应该在选择标签中包含“th:field”属性。
<select th:field="*{<somefield>}">
您的 Controller :
@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider ) throws ParseException {
但是,如果您想将选择选项的数据作为请求参数发送,则必须将名称添加到选择标签中。
<select th:name="<paramName>">
您的 Controller :
@PostMapping("/infopedidos")
public String UsuariosIntervaloSubmit(Model model, @ModelAttribute riders newRider, @RequestParam(value="<paramName>") String param) throws ParseException {
表格:
<form th:action="@{/<mapping>}" method="post">
<select class="form-control" name="<paramName>">
<option th:each="obj : ${objList}"
th:value="${obj.field}" th:text="${obj.field2}"></option>
</select>
Controller :
@PostMapping
public String func(@RequestParam("<paramName>") String param, Model model) {
** Your Code Here **
}
关于java - 在 Spring Thymeleaf 中获取值(value)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61098485/