onclick thymeleaf 动态 url 参数

2 spring-mvc thymeleaf

我是 Spring MVC 的初学者,我犯了一些错误......所以我想伪造一个带有按钮和 Thymeleaf 的动态 URL,但它不起作用。我想转义引号或类似的, ${key} 在评估中不正确?

<tr th:each="key: ${serverBean.getServerB().keySet()}">
    <td>
        <span th:text="${serverBean.getServerB().get(key)}" />
    </td>
    <td align="center">
        <span th:text="${key}" />
    </td>
    <td th:if="${protoStatusBean.getStatus(key)}" bgcolor="lime" />
    <td th:unless="${protoStatusBean.getStatus(key)}" bgcolor="red"/>
    <td>
        <button th:onclick="window.location.href='/update?server=${key}'">
            <img src="./images/wrench.png" height="15" width="15">
        </button>
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助和耐心

Met*_*ids 5

通常,您必须用单引号将文本文字括起来。为了让您的示例正常工作,它应该如下所示:

th:onclick="'window.location.href=\'/update?server=' + ${key} + '\''"
Run Code Online (Sandbox Code Playgroud)

话虽如此,还有多种其他方法可以让字符串连接工作,这取决于您认为最好的方式。

th:onclick="|window.location.href='/update?server=${key}'|"
th:onclick="${'window.location.href=''/update?server=' + key + ''''}"
th:onclick="|window.location.href='@{/update(server=${key})}'|"
Run Code Online (Sandbox Code Playgroud)