【问题标题】:Url with paramter as Button (Thymeleaf + Spring boot)参数为按钮的网址(Thymeleaf + Spring boot)
【发布时间】:2021-05-28 00:02:24
【问题描述】:

我正在使用 Spring Boot 和 Thymeleaf。如果用户单击按钮,则该按钮应向用户显示另一个页面。我唯一的问题是,url 需要一个参数。

我试过了,但这不起作用:

<form th:action="@{/removeserver(id=${serverId})}">
    <input  type="submit" value="Remove server"/>
</form>

它适用于以下链接,但不能用作按钮:

<a th:text="Remove" th:href="@{/removeserver(id=${serverId})}"></a>

那么如何将带有参数的 url 显示为按钮呢?

【问题讨论】:

  • 当你点击链接时浏览器执行GET请求,而form提交数据时默认使用POST方法。添加到您的表单method="GET" 并且您的代码应该可以工作。

标签: java html spring-boot spring-mvc thymeleaf


【解决方案1】:

我遇到了类似的问题,所以我没有使用路径变量,而是使用了请求参数:

<form action="archive?id=${x.id}" method="post">
    <button type="submit">Archive</button>
</form>

你可以试试这个。

【讨论】:

  • 它对我不起作用,这怎么可能起作用?您没有对变量使用百里香操作。
  • 试试这个:&lt;form th:action="@{/removeserver/{id}(id=${serverId})}"&gt; &lt;input type="submit" value="Remove server"/&gt; &lt;/form&gt;
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2018-10-02
  • 2017-05-26
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
相关资源
最近更新 更多