Thymeleaf:<form>中的POST或PUT:动作取决于条件

时间:2017-06-22 09:06:06

标签: java thymeleaf

是否可以在POST中分配PUTfrom th:action方法,具体取决于th:if

类似的东西:

<form th:if="${firm.firmId} == null" th:action="@{~/firms/save}" method="POST" modelAttribute="firm" th:object="${firm}">

<form th:if="${firm.firmId} != null" th:action="@{~/firms/save}" method="PUT" modelAttribute="firm" th:object="${firm}">

1 个答案:

答案 0 :(得分:4)

Thymeleaf有条件算子

condition ? first_expression : second_expression;

您可以将此运算符用于您的目的:

<form  th:action="@{~/firms/save}" th:method="${firm.firmId} != null ? PUT : POST" modelAttribute="firm" th:object="${firm}">