2014-02-16 51 views
0

我試圖找到一個乾淨的解決方案,以下反模式與 Thymeleaf /瓦應用:模式,以避免重複內容使用Thymeleaf和Apache瓷磚

我有2種形式(位於兩個不同現在的模板),它們與完全相同,但是對於指向不同URL的012行動屬性:

第一種形式

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

第二種形式是相同的,但對於日:action屬性是如下:th:action="@{/advertisement/family/edit}"

我想過很多方法可以解決我的這個反模式:

  1. 有兩種不同的形式,包括(identica l)用tiles:include表格內容。
  2. 尋找一種方法來以某種方式將th:action的值作爲變量。

不過,我會很感激,如果有人誰遇到了同樣的問題,可以建議我最好的做法...

回答

1

如果只有行動值是不同的,我會把動作 - 網址導入模型。

如果它更復雜,我會使用從thymelaef可以參數化的默認包含機制。

也許瓷磚在這種情況下不是最好的選擇。 Thymeleaf提供4 different solutions

+0

我遵循你的建議,並最終使用參數化。非常感謝! – balteo