0
votes

I have a typical updateUser form built with Thymeleaf + Spring. I can list the user roles but I am trying to put these into a selectbox component. I am struggling with this line th:selected="${user.hasRole(role.role)}". Now, I know this component works and that is only a matter of access a boolean function to enable it. I am trying to reference a function of the form object using the select th:object. My syntax doesn't work. I have also try to access that function just like previous input tags would (only using the name without the object itself .ie username or in this case hasRole(). That doesn't work either.

<form th:object="${user}"
th:action="@{'/admin/usermanagement/adduser'} " method="post">

    <div class="row">
        <div class="col-md-3 form-group">
            <label>Username:</label> <input type="text" class="form-control"
                th:field="*{username}" />
        </div>
    </div>
    <div class="col-md-3">
        <h5>Roles :</h5>
    </div>

    <select class="js-example-basic-multiple" style="width: 75%"
        name="froles" id="froles" multiple="multiple">
        <option th:each="role : ${roles}" th:value="${role.role}" 
        th:selected="${user.hasRole(role.role)}"
        th:text="${role.role}"></option>
    </select>

    <button type="submit" class="btn btn-primary">Submit</button>

1

1 Answers

0
votes

In fact, my problem is types. th:selected="${user.hasRole(role.role)}" is a string and should be th:selected="${user.hasRole(role)}". I have both method in the domain object so that is fine.

th:value="${role.role}" is wrong for persistence. I want to return role but I get the following error message when I use role.role codes [user.roles,roles]; arguments []; default message [roles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property

and Java null exception when I use "${role}"