使用Thymeleaf和Apache Tiles时避免重复内容的模式

时间:2014-02-16 11:21:10

标签: tiles thymeleaf

我正在寻求使用我的 Thymeleaf / Tiles应用程序找到以下反模式的干净解决方案:

我有两个表单(目前位于两个不同的模板中),它们完全相同但是对于指向不同网址的th:action属性

第一种形式

<form th:object="${advertisementInfo}" th:action="@{/advertisement/family/new}" method="post" class="form-horizontal">
    <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger form-group">
        <ul>
            <li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
        </ul>
    </div>
    <input type="hidden" th:field="*{advertisement.id}"/>
    <div th:class="${#fields.hasErrors('advertisement.needs')}? 'form-group error':'form-group'">
        <label class="control-label col-lg-3" for="needs" th:text="#{advertisement.family.form.needs}">Needs</label>
        <div class="col-lg-6">
            <select multiple="multiple" th:field="*{advertisement.needs}" class="form-control">
                <option th:each="need: ${needs}" th:value="${need}" th:text="#{${'domain.enum.need.' + need}}"></option>
            </select>
        </div>
    </div>

第二种形式是相同的,但对于th:action属性,如下所示:th:action="@{/advertisement/family/edit}"

我想到了解决这个反模式的几种方法:

  1. 有两种不同的形式,包括(相同的)表单内容和tiles:include
  2. 找到一种方法,以某种方式将th:action的值作为变量。
  3. 但是,如果遇到同样问题的人能向我提出最佳做法,我将非常感激......

1 个答案:

答案 0 :(得分:1)

如果只有action-value不同,我会将action-url放入模型中。

如果它更复杂,我会使用百里香的默认包含机制,可以进行参数化。

在这种情况下,瓷砖可能不是最好的选择。 Thymeleaf提供4 different solutions