Search code examples
thymeleaf

How to parse th:text with whitespace to selectbox in thymeleaf?


I want to use selectbox with this values:

  <select th:field="*{branch}" name="branch" class="form-control" id="branch" required autofocus>
      <option th:value="'WEAiI'"th:text="Elektrotechniki Automatyki i Informatyki"></option>          
      <option th:value="'WBiA'"th:text="Budownictwa i Architektury"></option>
  </select>

When I call my method I got this exception:

 Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could 
 not parse as expression: "Elektrotechniki Automatyki i Informatyki" 
 (template: "auth/register" - line 92, col 52)

When I use '_' instead of whitespace all is working correctly.

I tried successless use '&nbsp' like that:

 <option th:value="'WBiA'"th:text="Budownictwa&nbsp;i&nbsp;Architektury">

Is any way to parse expression with whitespace in select in thymeleaf?


Solution

  • Surround it with quotes, like you are doing for the value:

    <select th:field="*{branch}" name="branch" class="form-control" id="branch" required autofocus>
        <option th:value="'WEAiI'" th:text="'Elektrotechniki Automatyki i Informatyki'" />
        <option th:value="'WBiA'" th:text="'Budownictwa i Architektury'" />
    </select>
    

    But you really don't need to use th: attributes in this case. You could do something like this as well.

    <select th:field="*{branch}" name="branch" class="form-control" id="branch" required autofocus>
        <option value="WEAiI">Elektrotechniki Automatyki i Informatyki</option>          
        <option value="WBiA">Budownictwa i Architektury</option>
    </select>