Problems with url using Thymeleaf

Asked

Viewed 919 times

0

I have a page that shows all blog posts. This page has a posting object that in turn has the attribute that is a list of categories.

This list of categories is shown on the page in the form links to each post inserted on the page for the end user. The end user can click on a link and then it is directed to a page that contains all posts related to that category link theme.

I want to use the th:each to assemble the links.
Links are assembled but incorrectly!!

Here’s how he gets:

http://localhost:8084/spring-thymeleaf/categoria/?jpa

assuming the user clicked on the "jpa" link generated by the code below:

   <div th:each="categoria: ${postagem.categorias}">
          <a  href="#"th:href="@{/categoria/(${categoria.permalink})}">
          <span th:text="${categoria.descricao}"></span></a>
      </div>

I want to value the content of ${categoria.permalink} but without having that infamous interrogation!

To get to this my code I based on an example of the site itself Thymeleaf:

 <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
          <td th:text="${prod.name}">Onions</td>
          <td th:text="${prod.price}">2.41</td>
         ...
            <span th:text="${#lists.size(prod.comments)}">2</span> comment/s
            <a href="comments.html" 
               th:href="@{/product/comments(prodId=${prod.id})}" 
               th:unless="${#lists.isEmpty(prod.comments)}">view</a>
          </td>
        </tr>

That generates the following url:

http://localhost:8084/gtvg/product/comments?prodId=9

I wish it was possible to use the following code

  <div th:each="categoria: ${postagem.categorias}">
          <a  href="#"th:href="@{/categoria/${categoria.permalink}}">
          <span th:text="${categoria.descricao}"></span></a>
      </div> 

But it has the following output, when I click on the link "jpa":

http://localhost:8084/spring-thymeleaf-jsp/categoria/$%7Bcategoria.permalink%7D

Here’s the excerpt from my controller that handles the request:

@RequestMapping(value = "/categoria/{link}", method = RequestMethod.GET)
public ModelAndView postsByCategoria(@PathVariable("link") String link, ModelMap model) {

    List<Postagem> postagens = postagemService.findByCategoria(link);

    model.addAttribute("postagens", postagens);

    return new ModelAndView("posts.html", model);
}

What should I do? I’ve searched many places and nothing!!

  • Apparently the ? is coming straight from the value of ${categoria.permalink}. How this value is generated?

  • 2

    Try to use <a href="#" th:href="@{/categoria/{link}(link=${categoria.permalink})}">, has more in the documentation

  • @Andersoncarloswoss At last! Phew! Thank you very much!! Put as a reply compadre!! Ai fica explicito para quem passar pelo mesmo probleminha inha!!

1 answer

3


By default, as per documentation, the tool adds variables to the query string of the URL. If you want to enter the URL’s value, use the syntax {nomeDaVariavel}, according to the third example of the above-mentioned documentation.

For your example, I would:

<a href="#" th:href="@{/categoria/{link}(link=${categoria.permalink})}">

Where {link} is the insertion of the variable into the URL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.