3

So I'm trying to redirect the entire url with a th:href, but it is adding characters that I don't want.

My current url is this

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

And I'm trying to backtrack to the course the post was a part of, which is

http://localhost:8080/viewCourse/1

So currently this is what my html looks like

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

And this is the url I get

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

And the Id is correct, but I'm not sure why the ? is there.

I've also tried this

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

Which gives me this

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

If anybody can see how I can fix this and let me know that would be great, thanks in advance.

dochsner
  • 249
  • 2
  • 8
  • 25

2 Answers2

5

You can achieve adding id without question mark by String concatenation

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

However I would recommend to study this answer https://stackoverflow.com/a/14938399/5900967

As this can fail in some contexts

Community
  • 1
  • 1
Konrad Kostrzewa
  • 825
  • 7
  • 16
3

Apparently your id was added as a parameter.

Your code should be like this:

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

And the output should be like this:

http://localhost:8080/viewCourse/1

To learn more about thymeleaf url syntax, see https://www.thymeleaf.org/doc/articles/standardurlsyntax.html