【问题标题】:onclick thymeleaf dynamic url parameteronclick thymeleaf 动态url参数
【发布时间】:2017-11-27 20:29:34
【问题描述】:

我是 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>

感谢您的帮助和耐心

【问题讨论】:

    标签: spring-mvc thymeleaf


    【解决方案1】:

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

    th:onclick="'window.location.href=\'/update?server=' + ${key} + '\''"
    

    话虽如此,还有多种其他方法可以使字符串连接起作用,具体取决于您认为最好的方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多