有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring Boot+Bootstrap+Thymeleaf单选按钮

我在my 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"


共 (1) 个答案

  1. # 1 楼答案

    你在UserPayload类中输入了错误的类型。而不是:

    private System mainPicture;
    

    。。。你应该:

    private String mainPicture;
    

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

    编辑:

    我会把我的答案扩大一点

    我假设System被错误地用来代替String

    否则,例如System是你的一个类,那么你的问题就缩小到反序列化问题。有一个纯文本值从表单传递到控制器(i.a."7e92cc5c-73e7-451b-9ee8-2ae43c1b0125.jpg")。Java应该知道如何将这样的文本绑定到mainPicture变量。如果mainPicture属于您的任何自定义类型,那么它就不再是一个简单的赋值。也许一个接受单个String参数的构造函数就足够了