mercredi 18 mars 2015

How to repeat a Spring model in a Thymeleaf fragment

I have a Spring + Thymeleaf project. I have a fragment for the header part and some pages which include the header.


Below is the (essential part of) the header fragment. As you can see it includes the activeUserWorkgroup model object


dashboard-header.html



<!DOCTYPE html>
<html xmlns="http://ift.tt/lH0Osb"
xmlns:th="http://ift.tt/wfNV60">

<body>
<header th:fragment="dashboard-header" class="header">
....
<div class="navbar-right">
<ul class="nav navbar-nav">

<li class="dropdown notifications-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<span class="user-label" style="margin-left:5px;"
th:text="${activeUserWorkgroup.getWorkgroupLabel()}">
</span>
</a>

</li>
</ul>
</div>
</nav>
</header>
</body>

</html>


In every Spring view where I need the header fragment I add the line



<div th:replace="fragments/dashboard-header :: dashboard-header"></div>


The problem is that in every Controller of every View in which I need this header, I have to give the proper value to the activeUserWorkgroup model object. This is both uncomfortable and error prone.


Is there any way to let Spring give the proper model value to the activeUserWorkgroup object without having to write the code in every Controller?


Thank you


Aucun commentaire:

Enregistrer un commentaire