我有一個在ParentForm備份類中作爲母親和父親實例化了兩次的父類。 Web表單期望爲每個父驗證一個名字和姓氏爲NotNull。我創建了一個Thymeleaf片段專門爲「母親」的實例,但我想使片段一般在父實例:傳遞給Thymeleaf片段的引用對象
<div class="form-group" th:fragment="name">
<div class="form-row clearfix">
<div th:classappend="${#fields.hasErrors('mother.firstName') or #fields.hasErrors('mother.lastName')} ? has-error : ''">
<label class="control-label col-xs-12" th:classappend="${#fields.hasErrors('mother.firstName') or #fields.hasErrors('mother.lastName')} ? has-error : ''">What is the mother's name?</label>
</div>
<div class="col-sm-11 col-sm-offset-1 col-xs-12">
<div class="form-row form-group">
<div th:classappend="${#fields.hasErrors('mother.firstName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="mother.firstName" class="control-label">First </label>
<input type="text" th:field="*{mother.firstName}" th:value="*{mother.firstName}" class="form-control"/>
</div>
</div>
<div th:classappend="${#fields.hasErrors('mother.lastName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="mother.lastName" class="control-label">Last </label>
<input type="text" th:field="*{mother.lastName}" th:value="*{mother.lastName}" class="form-control"/>
</div>
</div>
</div>
</div>
</div>
<div th:if="${#fields.hasErrors('mother.firstName') or #fields.hasErrors('mother.lastName')}" class="form-row clearfix has-error">
<div class="help-block small">
<ul>
<li th:each="err : ${#fields.errors('mother.firstName')}" th:text="${err}"></li>
<li th:each="err : ${#fields.errors('mother.lastName')}" th:text="${err}"></li>
</ul>
</div>
</div>
</div>
父類具有firstName和lastName使用標準的命名約定的getter/setter方法一起。這一切都按預期工作 - 驗證,顯示,綁定。
因此,我試圖通過將簽名更改爲像th:fragment =「name(parent,parentType)」,並將「mother」實例替換爲「parent」實例,如下所示:
<div class="form-group" th:fragment="name(parent, parentType)">
<div class="form-row clearfix">
<div th:classappend="${#fields.hasErrors('parent.firstName') or #fields.hasErrors('parent.lastName')} ? has-error : ''">
<label class="control-label col-xs-12" th:classappend="${#fields.hasErrors('parent.firstName') or #fields.hasErrors('parent.lastName')} ? has-error : ''" th:text="|What is the ${parentType}'s name?|"></label>
</div>
<div class="col-sm-11 col-sm-offset-1 col-xs-12">
<div class="form-row form-group">
<div th:classappend="${#fields.hasErrors('parent.firstName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="parent.firstName" class="control-label">First </label>
<input type="text" th:field="*{parent.firstName}" th:value="*{parent.firstName}" class="form-control"/>
</div>
</div>
<div th:classappend="${#fields.hasErrors('parent.lastName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="parent.lastName" class="control-label">Last </label>
<input type="text" th:field="*{parent.lastName}" th:value="*{parent.lastName}" class="form-control"/>
</div>
</div>
</div>
</div>
</div>
<div th:if="${#fields.hasErrors('parent.firstName') or #fields.hasErrors('parent.lastName')}" class="form-row clearfix has-error">
<div class="help-block small">
<ul>
<li th:each="err : ${#fields.errors('parent.firstName')}" th:text="${err}"></li>
<li th:each="err : ${#fields.errors('parent.lastName')}" th:text="${err}"></li>
</ul>
</div>
</div>
</div>
在哪裏「父」是對象和「ParentType的」的實例只是用於顯示目的父類型(例如,「媽媽」或「父」)的字符串值。
<div th:replace="fragments/survey/mother::name(*{mother}, 'mother')"></div>
,我得到的,當我嘗試這樣的錯誤:
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#fields.hasErrors('parent.firstName') or #fields.hasErrors('parent.lastName')" (template: "fragments/survey/mother" - line 7, col 18)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('parent.firstName') or #fields.hasErrors('parent.lastName')" (template: "fragments/survey/mother" - line 7, col 18)
如何可以引用在#fields.hasErrors場()方法在一個通用的方式?
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'parent' of bean class [g.s.m.ParentForm]: Bean property 'parent' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
顯然,bean屬性被命名爲「媽媽」和「爸爸」,讓我怎麼得到這個通用的片段綁定到「媽媽」和「爸爸」,而不是「家長嗎?」
太棒了!感謝您對我的示例代碼的清晰解釋和更正。我用'__ $ {} __'語法來回移動,但找不到'#fields.hasErrors()'。我也很欣賞'th:value'和'th:field'用法的提示 –