Thymeleaf子对象未绑定

时间:2019-08-06 06:57:09

标签: spring thymeleaf

我有一个表格。创建或编辑用户。他们使用相同的形式。如果发送了用户对象,则将填充字段;如果用户为null,则这些字段为空。发送动作后,它会创建一个用户是否为空的用户,如果用户不为空,则会更新:

   <div id="user-details-div" th:fragment="add-edit-user-form-fragment">
        <form id="add-user-form" method="POST" th:action="@{/internal-user}" th:field="${user}">

            <div class="form-group">
                <input name="id" style="display:none"
                       th:value="${user} ? ${user.id} : ''"
                       type="text">
            </div>
        <div class="form-group">
            <input autocomplete="off" class="form-control" name="firstname"
                   th:value="${user} ? ${user.firstname} : ''"
                   placeholder="Ad" type="text">
        </div>
//some other fields

        <button id="add-edit-user-btn" class="btn btn-info" type="submit">Add edit</button>

通过这些字段,它可以完美运行。但是我还需要另一个领域。这是用户类别:

 private Long id;
  private String firstname;
//and this

    private UserStatusDTO status;

这是该子dto的类:

public class UserStatusDTO implements Serializable {
       private Long id;

        private String name;

因此,如果用户不为null,我也想添加此字段并使其预先选择。但是,即使我也无法绑定到后端字段。例如,

   <div class="form-group">
                <select class="form-control-file" id="select-user-role-drop-down"
                        name="status" th:field="${status.id}">
         </select>
        </div>

如果我只说这个,它说

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name

我也想放

status

status.id

*status

        <div class="form-group">
    <select class="form-control-file" id="select-user-role-drop-down"
            th:field="*{status}" >

但是他们都不起作用。为什么它不能在后端绑定到dto?+

如果我这样做,

    <div class="form-group">
            <select  th:valuetype="com.caner.ercan.backend.custom_model.UserStatusDTO"> class="form-control-file"
                     id="select-user-role-drop-down" name="status"
                   >
           <option th:each="status : ${allStatusList}"
                        th:text="${status.name}"
                        th:value="${status}">

                </option>
            </select>
        </div>

这一次说

Field error in object 'user' on field 'status': rejected value [UserStatusDTO{id=1, name='ACTIVE'}]

我也做了

th:value="${status.id}">

错误是

 Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'status': rejected value [UserStatusDTO{id=1, name='ACTIVE'}]; codes [typeMismatch.user.status,typeMismatch.status,typeMismatch.com.ercan.caner.backend.custom_model.UserStatusDTO,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.status,status]; arguments []; default message [status]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.ercan.caner.backend.custom_model.UserStatusDTO' for property 'status'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.ercan.caner.backend.custom_model.UserStatusDTO' for property 'status': no matching editors or conversion strategy found]]

0 个答案:

没有答案
相关问题