java - Thymeleaf:根据条件在 <form> th:action 中进行 POST 或 PUT

标签 java thymeleaf

是否可以分配 POSTPUT from 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}">

最佳答案

Thymeleaf 有条件运算符

condition ? first_expression : second_expression;

您可以使用此运算符来达到您的目的:

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

关于java - Thymeleaf:根据条件在 <form> th:action 中进行 POST 或 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694955/

相关文章:

java - 在java中对文件进行密码保护

java - Java嵌套队列

java - 与 Maven 集成的 Eclipse 动态 Web 项目

java - 使用 LuaJava 在 Java 项目中调试 Lua

android - Ubuntu 10.04 64 位上的 Android 开发使用哪个 JDK?

jquery - datetimepicker 值未与 thymeleaf 的属性 spring 绑定(bind)

javascript - 如何通过javascript在thymeleaf中根据数据库进行复选框

javascript - if 语句中的 JQuery AND OR 运算符

spring - 我们可以以 thymeleaf 形式发布 JWT token 吗

java - 如何使用 thymeleaf 格式化 HTML5 中的货币