2016-05-19 68 views
0

让我们有两种形式支持类。Spring MVC + Thymeleaf复合形式支持beans

Person 
    - name (String) 
    - surname (String) 

Application 
    - reason (String) 
    - date (Date) 
    - person (Person) 

和使HTML表单用于Person对象像这样的Thymeleaf片段:

<input name="name"/> 
<input name="surname"/> 

是否有posibility重用在这使得Application HTML形式与预期的结果的片段相同的片段:

<input name="reason"/> 
<input name="date"/> 
<input name="person.name"/> 
<input name="person.surname"/> 

换言之可以Thymeleaf形式片段被普遍的情况下使用时Person是顶层的形式支持bean,并且在它是其他形式支持bean的一部分的情况下?

回答

0

只需使用前缀并将其传递给您的模板。我希望你喜欢这个主意。在下面的代码中,应用程序片段也可以作为前缀并包含在另一个表单中。我没有测试过这些代码。如果有什么不对,请让我知道。我在我的代码中使用这样的构造。

<th:block th:fragment="person"> 
    Name:<input type="text" th:field="*{__${prefix + 'name'}__}" /> 
    Surname:<input type="text" th:field="*{__${prefix + 'surname'}__}" /> 
</th:block> 

<th:block th:fragment="application"> 
    Reason:<input type="text" th:field="*{__${prefix + 'reason'}__}" /> 
    Date:<input type="text" th:field="*{__${prefix + 'date'}__}" /> 
    <div th:replace="fragments/forms :: person" th:with="prefix=${prefix +'person.'}"> 
</th:block>