代码之家  ›  专栏  ›  技术社区  ›  en Lopes

Spring Boot+Bootstrap+Thymeleaf单选按钮

  •  0
  • en Lopes  · 技术社区  · 4 年前

    我在Thymeleaf模板中有这段代码:

    <form action="#"
          th:action="@{/update/{id}(id=${user.id})}"
          th:object="${user}"
          method="post"
          enctype="multipart/form-data">
    
     <tr th:each="pic: ${pics}" >
                            <td class="col_name" >
                                <div class="box small">
                                    <img th:src="'pics/' + ${user.id} + '/' + ${pic}"/>
                                </div>
                            </td>
                            <td class="col_actions">
                                <a style="color:#808080; margin-right: 10px;">
                                    <input type="radio" th:field="*{mainPicture}" th:value="${pic}" >Main picture<br>
                                </a>
                            </td>
                            <td class="col_actions">
                                <a href="#" style="color:#808080;  text-align: center;"  >
                                    <i class="fa fa-times" aria-hidden="true" ></i>
                                </a>
                            </td>
                        </tr>
    

    检查源代码:

    <td class="col_actions">
                                <a style="color:#808080; margin-right: 10px;">
                                    <input type="radio" value="7e7f6887-c0ce-4dc3-bb95-2b9ef92fc903.jpg" id="mainPicture1" name="mainPicture" >Main picture<br>
                                </a>
                            </td>
                            <td class="col_actions">
                                <a href="#" style="color:#808080;  text-align: center;"  >
                                    <i class="fa fa-times" aria-hidden="true" ></i>
                                </a>
                            </td>
                        </tr>
                        <tr >
                            <td class="col_name" >
                                <div class="box small">
                                    <img src="pics/5/7e92cc5c-73e7-451b-9ee8-2ae43c1b0125.jpg"/>
                                </div>
                            </td>
                            <td class="col_actions">
                                <a style="color:#808080; margin-right: 10px;">
                                    <input type="radio" value="7e92cc5c-73e7-451b-9ee8-2ae43c1b0125.jpg" id="mainPicture2" name="mainPicture" >Main picture<br>
                                </a>
                            </td>
    

    ...

    在控制器上:

    @PostMapping("/update/{id}")
        public String updateUser(@PathVariable("id") long id, @Valid UserPayload userPayload,
                BindingResult result, Model model) {
    
    
            System.out.println("===================");
            System.out.println( userPayload.getMainPicture());
            System.out.println("===================");
    
    ..
    }
    
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonInclude(NON_NULL)
    public class UserPayload implements Serializable {
    
        private Long id;
    
        // pics
        private System mainPicture;
    ...
    }
    

    但为空,在模板中我检查了id=“mainPicture2”

    0 回复  |  直到 4 年前
        1
  •  0
  •   Nimantha Thatkookooguy    3 年前

    你的打字错误 UserPayload 类。而不是:

    private System mainPicture;
    

    …你应该有:

    private String mainPicture;
    

    当您更改类型时,值会正确绑定 System.out.println( userPayload.getMainPicture()); 将打印 7e92cc5c-73e7-451b-9ee8-2ae43c1b0125.jpg intead null .

    编辑:

    我会稍微扩展一下我的答案。

    我以为 System 被错误地用以替代 String .

    否则,例如。 System 是你的一个类,那么你的问题就缩小到反序列化问题。表单向控制器传递了纯文本值(即。 "7e92cc5c-73e7-451b-9ee8-2ae43c1b0125.jpg" ). Java应该知道如何将这样的文本绑定到您的 mainPicture 变量。如果 主图片 如果是你的任何自定义类型,那么它就不再是一个简单的任务了。可能是一个接受单曲的建筑商 String 争论就足够了。