Problems with url using thymeleaf

0

I have a page that shows all the posts of a blog. This page has a post 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 in 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 the posts related to that category link theme.

I want to use th:each to mount links. The links are assembled, but incorrectly !!

Here's how it goes:

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

Assuming the user clicked 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 put the contents of ${categoria.permalink} as value but without having that infamous query!

To get to 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 wanted to be able 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 the link "jpa":

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

Here is the portion of 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 already searched in several places and nothing !!

    
asked by anonymous 22.03.2017 / 01:04

1 answer

3

By default, according to documentation , the tool adds the variables to the query string of the URL. If you want to embed the value of the same in the URL, use the {nomeDaVariavel} syntax, according to the third example of the above documentation.

For your example, it would look like:

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

Where {link} is the embodiment of the variable in the URL.

    
22.03.2017 / 01:43