Can not bind child object to parent from thymeleaf

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Can not bind child object to parent from thymeleaf

CursedChico
   

this is **userstatusdto**

    com.caner.ercan.backend.custom_model.UserStatusDTO
    public class UserStatusDTO implements Serializable {
   
        private Long id;
   
        private String name;
   
    //getters setters

 **userdto** has this:

    private UserStatusDTO status;



I want to send user dto from thymeleaf front end:


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

but it gives error which i wrote in the  beginning. What can be reason?
Should i send id , so i should create another dto?


**allstatuslist** is coming from model at first:

    List<UserStatusDTO>

so when i create new one (user = null), it goes here:

                <option th:each="status : ${allStatusList}"
                    th:text="${status.name}"
                    th:value="${status}"
                    th:valuetype="com.caner.ercan.backend.custom_model.UserStatusDTO">

I also tried this

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


but this time, HTMl did not show that div :(


I got this error


> Field error in object 'user' on field 'status': rejected value
> [UserStatusDTO{id=1, name='ACTIVE'}]; codes
> [typeMismatch.user.status,typeMismatch.status,typeMismatch.com.caner.ercan.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.caner.ercan.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.caner.ercan.backend.custom_model.UserStatusDTO' for property
> 'status': no matching editors or conversion strategy found]]

Also only if leave this and comment others:


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

it gives error

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'status' available as request attribute
Reply | Threaded
Open this post in threaded view
|

Re: Can not bind child object to parent from thymeleaf

nhatanh2996
the valuetype make u got error
i think u need to remove " th:valuetype="com.caner.ercan.backend.custom_model.UserStatusDTO">" and run or change valuetype to exactly with ur param