Interface IStandardConversionService

  • All Known Implementing Classes:
    AbstractStandardConversionService, StandardConversionService

    public interface IStandardConversionService

    Common interface for all implementations of a conversion service, to be used during template execution.

    Thymeleaf conversion services work in a way similar to Spring Framework's ConversionService interface, but this is a generic mechanism (not dependent on Spring).

    Default implementation —registered by StandardDialect— is StandardConversionService, which performs some standard conversions, but the Spring Standard Dialect used by the Thymeleaf + Spring integration module automatically registers an implementation of this interface that delegates on any existing Spring ConversionService objects (thus using the Converters and Formatters registered at the Spring Application Context).

    Important: there is one conversion that implementations of this interface should always implement, because it is heavily used at the Thymeleaf core: conversion of any Object to String.

    The implementation of this interface that should be used is specified as an execution attribute by the Standard Dialects (see StandardDialect.getExecutionAttributes()).

    Implementations of this interface should be thread-safe.

    Since:
    2.1.0
    Author:
    Daniel Fernández
    • Method Detail

      • convert

        <T> T convert​(IExpressionContext context,
                      Object object,
                      Class<T> targetClass)

        Convert a value to the specified target class, if possible.

        Might raise an exception (usually IllegalArgumentException) if a conversion is not available for the specified object and the target class.

        Type Parameters:
        T - the type of the target class
        Parameters:
        context - the context object.
        object - the object to be converted.
        targetClass - the target class the object should be converted to.
        Returns:
        the object, converted. Or an exception if the conversion has not been possible.