Simple Expressions

Messages

Zugriff auf Messages

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

Nicht-Statische Messages

Messages müssen nicht statisch sein. Hier das Beispiel aus einem .properties-File mit einer nicht-statischen message:

home.welcome=¡Bienvenido a nuestra tienda de comestibles, {0}!

Hinweis: Es sind auch noch komplexere Messages möglich (Siehe API für java-text-MessageFormat)

So wird die nicht-statische Message im HTML aufgerufen:

<p th:utext="#{home.welcome(${session.user.name})}">
  Welcome to our grocery store, Sebastian Pepper!
</p>

Variabeln anpassen

Variabeln können im Java-Code folgendermassen angepasst werden:

WebContext ctx = new WebContext(request, response, servletContext, request.getLocale());
ctx.setVariable("today", dateFormat.format(cal.getTime()));

der dazugehörige HTML-Code sieht dann so aus:

<p>Today is: <span th:text="${today}">13 February 2011</span></p>

instead of a #{...} expression value, we are using a ${...} one. This is a variable expression value, and it contains an expression in a language called OGNL (Object-Graph Navigation Language) that will be executed on the context variables map. The ${today} expression simply means “get the variable called today”, but these expressions could be more complex (like ${user.name} for “get the variable called user, and call its getName() method”).

${...} expressions are in fact OGNL (Object-Graph Navigation Language) expressions

Ein OGNL-Aufruf wie

${session.user.name}

ersetzt den folgenden Code:

((User) ctx.getVariables().get("session").get("user")).getName();

-> die '.'-Syntax ruft jeweils die Getter auf.

Object Selection

Wenn man immer wieder Attribute von selben Objekt benötigt, kann die object-selection-syntax verwendet werden:

<div th:object="${session.user}">
  <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
  <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

Auch Links mit Attributen können mit Thymeleaf vereinfacht werden:

<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
  • If several parameters are needed, these will be separated by commas like @{/order/process(execId=${execId},execType='FAST')}
  • Variable templates are also allowed in URL paths, like @{/order/{orderId}/details(orderId=${orderId})}

URL's können auch das Resultat einer anderen Expression sein.

<a th:href="@{${url}(orderId=${o.id})}">view</a>
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>

Serer-Root-Relative URLs werden folgendermassen spezifiziert:

These URLs will be specified like @{~/path/to/something}

results matching ""

    No results matching ""