Spring mvc 传递给Thymeleaf片段的引用对象
我有一个父类,在ParentForm支持类中作为母亲和父亲实例化了两次。web表单要求为每个父级验证名和姓,并将其设置为NotNull。我专门为“母亲”实例创建了一个Thymeleaf片段,但我想使该片段对两个父实例都通用:Spring mvc 传递给Thymeleaf片段的引用对象,spring-mvc,parameter-passing,thymeleaf,Spring Mvc,Parameter Passing,Thymeleaf,我有一个父类,在ParentForm支持类中作为母亲和父亲实例化了两次。web表单要求为每个父级验证名和姓,并将其设置为NotNull。我专门为“母亲”实例创建了一个Thymeleaf片段,但我想使该片段对两个父实例都通用: <div class="form-group" th:fragment="name"> <div class="form-row clearfix"> <div th:classappend="${#fields.has
<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>
如何以通用方式引用#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属性被命名为“母亲”和“父亲”,那么我如何让这个通用片段绑定到“母亲”和“父亲”而不是“父亲?”因此,首先您必须意识到,对于像
th:field
和\fields.hasrerrors()
这样的内容,字符串结果必须匹配到支持对象的完整路径(而不是像parent
)这样的局部变量)。您不能使用*{parent.firstName}
,因为ParentForm没有getParent().getFirstName()。为此,必须使用
您所需要的不是将对象传递给片段,而是对象名。(在您的情况下,由于parentType已经有了母亲或父亲,因此我将在示例中使用它们。)进行这些更改后,您的字段应该如下所示:
<div th:classappend="${#fields.hasErrors(parentType + '.firstName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="${parentType + '.firstName'}" class="control-label">First </label>
<input type="text" th:field="*{__${parentType}__.firstName}" class="form-control"/>
</div>
</div>
<div th:classappend="${#fields.hasErrors(parentType + '.lastName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="${parentType + '.lastName'}" class="control-label">Last </label>
<input type="text" th:field="*{__${parentType}__.firstName}" class="form-control"/>
</div>
</div>
第一
最后的
另外,一个旁注…如果您使用的是
th:field
,那么您不必使用th:value
th:field
。太棒了!感谢您对我的示例代码进行了清晰的解释和更正。我反复使用{}{/code>语法,但无法理解\fields.hasErrors()
。我也很欣赏关于th:value
和th:field
用法的提示
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)
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?
<div th:classappend="${#fields.hasErrors(parentType + '.firstName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="${parentType + '.firstName'}" class="control-label">First </label>
<input type="text" th:field="*{__${parentType}__.firstName}" class="form-control"/>
</div>
</div>
<div th:classappend="${#fields.hasErrors(parentType + '.lastName')} ? has-error : ''" class="col-xs-12 col-sm-6">
<div class="input-group">
<label th:for="${parentType + '.lastName'}" class="control-label">Last </label>
<input type="text" th:field="*{__${parentType}__.firstName}" class="form-control"/>
</div>
</div>