title: How to static initialize a Map in Thymeleaf?
body: <p>I have the following <code>thymeleaf</code> navigation bar html, and would like to shorten it by repeating the link creation with <code>th:each</code> from a static <code>Map</code>:</p>
  
  <pre><code>&lt;div id="navbar" class="collapse navbar-collapse"&gt;
      &lt;ul class="nav navbar-nav"&gt;
          &lt;li th:classappend="${#httpServletRequest.getRequestURI() == '/home' ? 'active':''}"&gt;
              &lt;a href="/home"&gt;Home&lt;/a&gt;&lt;/li&gt;
          &lt;li th:classappend="${#httpServletRequest.getRequestURI() == '/pax' ? 'active':''}"&gt;
              &lt;a href="/pax"&gt;Person&lt;/a&gt;&lt;/li&gt;
          &lt;li th:classappend="${#httpServletRequest.getRequestURI() == '/about' ? 'active':''}"&gt;
              &lt;a href="/about"&gt;About&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The question is: how can I initialize a static <code>Map</code> inside the html page, without having to bind to a backend controller? The map should key-value represent the mapping between "url path" and "link name".</p>
  
  <p>In java code this would be:</p>
  
  <pre><code>new HashMap&lt;String, String&gt;() {{
     put("/pax", "Person");
  }};
  </code></pre>
  
  <p>But how can I achieve this in plain thymeleaf html, without backend code?</p>
  
  <p>Similar to:</p>
  
  <pre><code>&lt;li th:each="entry : ${HOW TO HASHMAP}"
      th:classappend="${#httpServletRequest.getRequestURI() == '/'+entry.getKey()' ? 'active':''}"&gt;
    &lt;a th:href="'/'+${link.getKey()}" th:text="$entry.getValue()"/&gt;
  &lt;/li&gt;
  </code></pre>