0

I am making web app using Java/Spring/Thymeleaf and don't seem to be able to get past this problem!

So, here's my thymeleaf template code:

<form th:action="@{/holiday/create}" th:object="${holiday}" method="post">
    <select name="user_scroll">
        <option th:each="user : ${allUsers}" th:value="${user.id}"
                th:text="${user.email}" th:field="${holiday.user_id}" />
    </select>
    <button type="submit">
        Create
    </button>
</form>

I have read the related questions on here but am still stumped. I did read that you can't bind an object directly that's why I am trying to bind to the user_id property of the holiday object. I did suspect the holiday object my be out of scope but that doesn't seem to be the case. Perhaps I am accessing the user_id property incorrectly?

When I click submit and follow debug through to my controller user_id is just sent through as null. I hope that's enough info - let me know if I need to provide more. Thanks!

1
  • What is the exact problem you have? Commented May 14, 2015 at 8:56

1 Answer 1

1

Frankie, try adding the th:field attribute to the select object instead of putting it in the option. Like this:

 <select name="user_scroll" th:field="*{user_id}">

Notice the SPel syntax. Starts with an asterisk instead of $ and says user_id instead of holiday.user_id. You can access the field directly this way, since you already have defined it as your selected object in the form definition using

th:object="${holiday}" 

This will also bind the selected value to the user_id property of your backing object and should solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.