【问题标题】:Spring MVC + Thymeleaf composite form backing beansSpring MVC + Thymeleaf 复合表单 backing beans
【发布时间】:2016-05-19 08:12:49
【问题描述】:

让我们有两个表单支持类。

Person
  - name (String)
  - surname (String)

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

还有一个 Thymeleaf 片段,它为 Person 对象呈现 html 表单,如下所示:

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

是否有可能在片段中重用相同的片段,以预期结果呈现Application html 表单:

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

换句话说,在Person是顶级表单支持bean的情况下以及在它是其他表单支持bean的一部分的情况下,Thymeleaf表单片段可以通用吗?

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    只需使用前缀并将其传递给您的模板。我希望你喜欢这个主意。在下面的代码中,应用程序片段也可以添加前缀并包含在另一种形式中。我没有测试过代码。如果有什么问题,请告诉我。我在我的代码中使用了这样的结构。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2017-11-13
      • 2016-03-13
      • 1970-01-01
      • 2018-01-07
      相关资源
      最近更新 更多