Frage

I am seeking to find a clean solution to the following antipattern with my Thymeleaf/Tiles app:

I have two forms (located in two different templates as of now) which are completely identical but for the th:action attributes which point to different urls.

First form:

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

Second form is identical but for the th:action attribute which is as follows: th:action="@{/advertisement/family/edit}"

I thought of several ways to address this antipattern of mine:

  1. Having two different forms and including the (identical) form content with a tiles:include.
  2. Finding a way to have the value for the th:action as a variable somehow.

However, I would be very grateful if someone who has met the same issue could suggest a best practice to me...

War es hilfreich?

Lösung

If only the action-value is different, I would put the action-url into the model.

If it more complex I would use the default include-mechanism from thymelaef which can be parametrized.

Perhaps tiles isn't the best choice in this situation. Thymeleaf offers 4 different solutions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top