I'm attempting to implement a Thymeleaf fragment containing a form, and having trouble passing in the th:object value.
The fragment:
<div th:fragment="editCard(colSize, title, formObject, formAction, fields)" th:class="${'col-lg-' + colSize}">
<div class="card">
<div class="card-header d-flex align-items-center">
<h4 th:text="${title}"></h4>
</div>
<div class="card-body">
<form th:object="${__${formObject}__}" th:action="${formAction}" method="post" class="form-horizontal">
<div th:replace="${fields}"></div>
</form>
</div>
</div>
</div>
And the invocation:
<div th:replace="fragments/cards2 :: editCard(colSize='12', title='Concert', formObject=${concert}, formAction='/concert/save', fields=~{ :: .fields})" >
<div class="fields">
<input type="hidden" th:field="*{id}"/>
<!--/*/ <th:block th:include="fragments/elements :: editSelect(label='Season', val='x', field='season', selectList='${seasons}', itemText='${item}', autofocus='autofocus')"></th:block> /*/-->
<!--/*/ <th:block th:include="fragments/elements :: editText(label='Title', val='x', field='title', autofocus='')"></th:block> /*/-->
<!--/*/ <th:block th:include="fragments/elements :: editCheckbox(label='Tour', field='tour')"></th:block> /*/-->
</div>
</div>
</div>
There seems to be an interaction between Thymeleaf and Spring binding that prevents the formObject from being accurately passed. I'm not sure exactly which Thymeleaf syntax combination between caller and fragment is correct.
You should pass the string 'concert'
instead of the object in this case. It's a bit weird, but it should work for you.
<div th:replace="fragments/cards2 :: editCard(colSize='12', title='Concert', formObject='concert', formAction='/concert/save', fields=~{ :: .fields})" >
and keep the rest the same. What you really want is for the expression th:object="${__${formObject}__}"
to evaluate to th:object="${concert}"
(which passing a string will do). Right now it's evaluating to something weird like th:object="${java.class.whatever@123412}"
(the default toString
for ${concert}
).
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加