Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
Conditions in Thymeleaf th:href parameter

So I have an element in my Thymeleaf template which has a link looking like that:

th:href="@{'/search/'+${searchType}(parameter1=${parameter1}, ... parameter10=${parameter10})}"

According to the current implementation the parameter1...10 can have values that I don't want to include in the href. For example, if it's id, it can be -1, which means that no id chosen, so this parameter have to be omitted to avoid clattering the url string, so instead of

/search/type?parameter1=-1

get just clean

/search/type

How this can be achieved? I tried this:

th:href="@{'/search/'+${searchType}((${parameter1}=='-1'?''parameter1=${parameter1}))}"

But this expression causes exception:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:

I tried this:

th:href="@{'/search/'+${searchType}(parameter1=(${parameter1}=='-1'?'':${parameter1}))}"

But then the result is still not acceptable:

/search/type?parameter1=

So what is the correct way to eliminate undesirable parameters from the url?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Looks like I did more research and found a way. The key word is: preprocessing. This is the link. So for my purposes the following expression works:

th:href="@{'/search/'+${searchType}(__(${parameter1}=='-1'?','parameter1=${parameter1})__,__(${parameter2}=='-1'?','parameter2=${parameter2})__)}"

Short explanation: surround the expression with double underscores __(${parameter1}=='-1'?','parameter1=${parameter1})__, and then the surrounded expression will only be added if it evaluates to non-empty string. In the case of url parameters ',' evaluates to two empty parameters and not added.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda