i have a console app that uses thymeleaf to generate email templates.
from my understanding, only the spring template engine is able to utilize conversion services to apply global formatting on thymeleaf context variables.
how do i register my conversion service with the spring template engine?
// 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);
turns out that spring template engine is not needed at all. I just needed to add my conversion service to the thymeleaf default dialect:
Set<IDialect> dialects = this.templateEngine.getDialects();
StandardDialect standardDialect = (StandardDialect) dialects.iterator().next();
IStandardConversionService conversionService = new MyConversionService();
standardDialect.setConversionService(conversionService);
in my conversion service, i used my converter. if it can't convert the object, i fallback to the default converter:
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);
}
}
then in my template, use the double brace syntax to apply the conversion:
${{variable}}
the thymeleaf spring dependency is still required because the converter interfaces are part of spring:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
If you are using spring framework + thymeleaf, is easy to add converters to yours projects, by simple create a subclass that´s extend from Formatter, and then register your class as bean in your WebMvcConfigurerAdapter class and overriden the method 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();
}
}
and then you can use in your view like this ${{variable}}
You can see that's docs http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#configuring-a-conversion-service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With