Spring Conversion Service

Kann zum Beispiel Benutzt werden, um ein formatiertes Datum auszugeben:

In the first column of the table listing we will show the date when the seed starter was prepared. But we will show it formatted in the way we defined in our DateFormatter . In order to do that we will use the double-bracket syntax, which will automatically apply the Spring Conversion Service.

<td th:text="${{sb.datePlanted}}">13/01/2011</td>

Syntax

Der Conversion Service funktioniert mit einer double-bracket syntax

The Conversion Service can be easily applied in order to convert/format any object into String. This is done by means of the double-bracket syntax:

  • For variable expressions: $
  • For selection expressions: *

So, for example, given an Integer-to-String converter that adds commas as a thousands separator, this:

<p th:text="${val}">...</p>
<p th:text="${{val}}">...</p>

…should result in:

<p>1234567890</p>
<p>1,234,567,890</p>

We saw before that every th:field attribute will always apply the conversion service, so this:

<input type="text" th:field="*{datePlanted}" />

…is actually equivalent to:

<input type="text" th:field="*{{datePlanted}}" />

Note that this is the only scenario in which the Conversion Service is applied in expressions using single-bracket syntax.

Das conversion utility objekt

The #conversions expression utility object allows the manual execution of the Conversion Service wherever needed:

<p th:text="${'Val: ' + #conversions.convert(val,'String')}">...</p>

Syntax für das utility objekt:

  • #conversions.convert(Object,Class) : converts the object to the specified class.
  • #conversions.convert(Object,String) : same as above, but specifying the target class as a String (note the java.lang. package can be ommitted).

Konfiguration

Die Konfiguratoin für den Conversion Service als XML befindet sich auf der Seite 27 im PDF.

results matching ""

    No results matching ""