th:href Thymeleaf 使用 Spring MVC 重定向带有路径变量和对象 ID 的 url

doc*_*ner 3 spring thymeleaf

因此,我尝试使用 th:href 重定向整个网址,但它添加了我不想要的字符。

我当前的网址是这个

http://localhost:8080/viewCourse/post/5
Run Code Online (Sandbox Code Playgroud)

我试图回溯到该帖子所属的课程,即

http://localhost:8080/viewCourse/1
Run Code Online (Sandbox Code Playgroud)

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

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

这是我得到的网址

http://localhost:8080/viewCourse/?1
Run Code Online (Sandbox Code Playgroud)

Id 是正确的,但我不确定为什么会?在那里。

我也尝试过这个

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

这给了我这个

http://localhost:8080/viewCourse/?id=1
Run Code Online (Sandbox Code Playgroud)

如果有人能看到我如何解决这个问题并让我知道那就太好了,提前致谢。

Kon*_*ewa 5

通过字符串连接可以实现添加无问号的id

<a th:href="@{/viewCourse/} + ${post.course.id}"><span th:text="${post.course.name}"></span></a>
Run Code Online (Sandbox Code Playgroud)

不过我建议研究这个答案/sf/answers/1045687961/

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