java - 在 Spring Thymeleaf 中获取值(value)的问题

标签 java spring spring-boot thymeleaf

我在尝试从帖子中获取值时遇到问题:

这是我的 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/

相关文章:

java - 如何在标准API中编写DDL?

Java - 创建伪 native 类型

java - 项目 react 器: block() after collectList() doesn't work for Flux. create()

java - 将外部 .jar 添加到 android studio 项目

java - QPID - Spring CachingConnectionFactory - 重新连接

java - Spring Boot 连接两个具有无效 FK 条目的实体

spring-boot - Thymeleaf 如何查看当前 URL?

spring - 在spring mvc中放置辅助类的位置

java - spring boot war 找不到index.html

java - 类型 3 UUID 冲突概率与 MD5 相同吗