【问题标题】:th:href Thymeleaf redirect url with path variables and object id's with Spring MVCth:href Thymeleaf 重定向 url 与路径变量和对象 id 与 Spring MVC
【发布时间】:2016-05-18 21:10:06
【问题描述】:

所以我尝试使用 th:href 重定向整个 url,但它添加了我不想要的字符。

我现在的网址是这个

http://localhost:8080/viewCourse/post/5

我正在尝试回溯到该帖子所属的课程,即

http://localhost:8080/viewCourse/1

所以目前这就是我的 html 的样子

<a th:href="@{'/viewCourse/'(${post.course.id})}"><span th:text="${post.course.name}"></span></a>

这是我得到的网址

http://localhost:8080/viewCourse/?1

ID 是正确的,但我不确定为什么会有 ?

我也试过了

<a th:href="@{'/viewCourse/'(id=${post.course.id})}"><span th:text="${post.course.name}"></span></a>

这给了我这个

http://localhost:8080/viewCourse/?id=1

如果有人能看到我如何解决这个问题并告诉我,那就太好了,在此先感谢。

【问题讨论】:

    标签: spring thymeleaf


    【解决方案1】:

    可以通过字符串拼接实现不带问号的加id

    <a th:href="@{/viewCourse/} + ${post.course.id}"><span th:text="${post.course.name}"></span></a>
    

    不过我建议研究一下这个答案https://stackoverflow.com/a/14938399/5900967

    因为这在某些情况下可能会失败

    【讨论】:

    • 谢谢,它成功了,也感谢其他答案,我会研究它。
    【解决方案2】:

    显然您的 id 已作为参数添加。

    你的代码应该是这样的:

    <a th:href="@{/viewCourse/{id}(id=${post.course.id})}"><span th:text="${post.course.name}"></span></a>
    

    输出应该是这样的:

    http://localhost:8080/viewCourse/1
    

    要了解有关 thymeleaf url 语法的更多信息,请参阅https://www.thymeleaf.org/doc/articles/standardurlsyntax.html

    【讨论】:

      猜你喜欢
      • 2016-02-18
      • 2014-07-10
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2023-03-31
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多