so here's my problem. I'm trying to have dynamic data in my header. It's supposed to represent the number of items a user has in his cart. So here's my header (the header is a fragment that I included in the layout.html) and the data is in the last li
<nav >
<ul>
<li><a th:href="@{/index}" class="">Home</a></li>
<li><a th:href="@{/products/list}" class="">Products</a></li>
<li><a th:href="@{/admin/index}" class="">Admin</a></li>
<li><a href="" class="">Account</a></li>
<li><a href="" class="">Contacts</a></li>
<li><a th:href="@{/login}" class="">Login</a></li>
<li><a th:href="@{/user-page}" class="">User</a></li>
<li>
<p th:text="${cart.id}"></p>
</li>
</ul>
</nav>
But since the header is available on every page of the web app, what should I do (in the controllers) to get this data? I tried to put the cart data in the IndexController but it doesn't work. I'm not comfortable with Spring and Thymeleaf so I might be doing something wrong or maybe I missed something on data binding. I only have once constraint, I don't want to use Ajax or Jquery. Unless I have no choice but I doubt that. Here's what I tried in the IndexController
@GetMapping({"/", "/index", "/home"})
public String getIndex(Model model) {
this.userCart.setId(5);
this.userCart.setNumberItems(11);
model.addAttribute("cart", userCart);
model.addAttribute("phrase", "Oui oui si si 92izi");
model.addAttribute("nbItems", "10");
return "index";
}
I also tried to make another method mapped to "/" but it causes an error since I can't have to methods mapped to the same url. So what am I supposed to do ?
Aucun commentaire:
Enregistrer un commentaire