Search code examples
springthymeleaf

Referring to the form backing bean itself in Spring Thymeleaf?


If I use a form-backing bean in Spring Thymeleaf, is there a way I can refer to the object itself?

Example:

<form action="/blah" th:object="${myObject}">
    A field value: [[ *{fieldValue} ]]
    The bean itself the regular way: [[ ${myObject} ]]
    The bean itself using *: [[ *{what_do_I_put_here} ]]
</form>

Solution

  • You can use the special object #object in ${...} expression to access a th:object (without having to know it's name on the model).

    <form action="/blah" th:object="${myObject}">
        A field value: [[ *{fieldValue} ]]
        The bean itself the regular way: [[ ${myObject} ]]
        The bean itself using #object: [[ ${#object} ]]
        A field value using #object: [[ ${#object.fieldValue} ]]
    </form>