Search code examples
spring-bootthymeleaf

Setting up thymeleaf value to anchor tag and null check


I am setting thymeleaf anchr tag href as below to use query param,

<a th:href="@{'/page?vars='+${param.q1}+'&varr=' + ${param.q2}}">

the above is working fine. But sometimes, I get q1 url param will be null. In that case, I need to set default value.

I have tried like,

<a th:href="@{'/page?vars='+${param.q1 != null} ? ${param.q1} : 'default' +'&varr=' + ${param.q2}}">

But I am getting error on this page. Any suggestions on this?


Solution

  • You should be using Thymeleaf's standard URL syntax when generating links (rather than string concatenation). This expression should work for you:

    <a th:href="@{/page(vars=${param.q1} ?: 'default',varr=${param.q2})}">