如何为SpringTemplateEngine注册转换服务?

how to register conversion service for SpringTemplateEngine?

我有一个使用 thymeleaf 生成电子邮件模板的控制台应用程序。

据我了解,只有 spring 模板引擎能够利用转换服务对 thymeleaf 上下文变量应用全局格式。

如何使用 spring 模板引擎注册我的转换服务?

// init the template engine
templateEngine = new SpringTemplateEngine();
templateResolver = new ClassLoaderTemplateResolver();
templateEngine.addTemplateResolver(templateResolver);

// generate the template
Context ctx = new Context(locale);
// i would like, for example, to format dates
ctx.setVariable("date", new Date());
String text = this.templateEngine.process(templateName, ctx);

事实证明根本不需要 spring 模板引擎。我只需要将我的转换服务添加到 thymeleaf 默认方言:

Set<IDialect> dialects = this.templateEngine.getDialects();
StandardDialect standardDialect = (StandardDialect) dialects.iterator().next();
IStandardConversionService conversionService = new MyConversionService();
standardDialect.setConversionService(conversionService);

在我的转换服务中,我使用了我的转换器。如果它不能转换对象,我回退到默认转换器:

public MyConversionService implements IStandardConversionService {
    GenericConversionService myConverter = new MyConverter();
    StandardConversionService standardConversionService = new StandardConversionService();

    @Override
    public <T> T convert(Configuration configuration, IProcessingContext processingContext, Object object, Class<T> targetClass) {

        if (myConverter.canConvert(object.getClass(), targetClass)) {
            return myConverter.convert(object, targetClass);
        }

        return standardConversionService.convert(configuration, processingContext, object, targetClass);
    }
}

然后在我的模板中,使用双括号语法应用转换:

${{variable}}

仍然需要 thymeleaf spring 依赖项,因为转换器接口是 spring 的一部分:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

如果您使用的是 spring 框架 + thymeleaf,则很容易将转换器添加到您的项目中,只需创建一个从 Formatter 扩展的子class,然后注册您的 class 作为 WebMvcConfigurerAdapter 中的 bean class 并覆盖方法 addFormatters(FormatterRegistry registry)

public class DateFormatter implements Formatter<Date> {

    public DateFormatter() {
        super();
    }

    @Override
    public Date parse(final String text, final Locale locale) throws ParseException {
        final SimpleDateFormat dateFormat = createDateFormat(locale);
        return dateFormat.parse(text);
    }

    @Override
    public String print(final Date object, final Locale locale) {
        final SimpleDateFormat dateFormat = createDateFormat(locale);
        return dateFormat.format(object);
    }

    private SimpleDateFormat createDateFormat(final Locale locale) {
        final String format = "dd/MM/yyyy";
        final SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
        dateFormat.setLenient(false);
        return dateFormat;
    }

}

--

@EnableWebMvc
@Configuration
public class ConfigurationWeb extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    @Autowired
    MapeadorObjetos mapeadorObjetos;

    @Autowired
    Environment env;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        super.addFormatters(registry);
        registry.addFormatter(dateFormatter());
    }

    @Bean
    public DateFormatter dateFormatter() {
        return new DateFormatter();
    }
}

然后您可以在您的视图中使用 ${{variable}}

你可以看到那是文档 http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#configuring-a-conversion-service