Thymeleaf 按钮在新选项卡中使用参数调用 url

Thymeleaf button call url with parameters in new tab

如何在 thymeleaf 中创建一个按钮作为 link 并使用一些给定参数在新选项卡中打开?

我试过这个但现在可以了:

<button th:onclick="window.open('https://www.google.com/location/' + ${id})"> GO</button>

当我使用变量时它不起作用(${id})

您可以像下面这样尝试。 bootstrap 将使您的锚标记看起来像一个按钮。

<a class="btn" th:href="@{/url/{id}(id=${id})}" target="_blank">GO</a>

如果你不使用bootstrap,我会这样表示按钮。

<button
    th:data-url="@{https://www.google.com/location/{id}(id=${id})}"
    onclick="window.open(this.getAttribute('data-url'))">GO</button>

注意:我使用的是 onclick 而不是 th:onclick

如果您使用 bootstrap,您可以像其他答案一样将 link 设置为按钮,只需使用 th:href.

<a class="btn" th:href="@{https://www.google.com/location/{id}(id=${id})}" target="_blank">GO</a>