如何为SpringTemplateEngine注册转换服务?

时间:2015-01-01 16:20:49

标签: java spring thymeleaf

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

根据我的理解,只有spring模板引擎能够利用转换服务在百里香叶上下文变量上应用全局格式。

如何使用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);

2 个答案:

答案 0 :(得分:2)

结果表明根本不需要弹簧模板引擎。我只需要将我的转换服务添加到百里香的默认方言:

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}}
由于转换器接口是弹簧的一部分,因此仍然需要百日咳弹簧依赖性:

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

答案 1 :(得分:0)

如果你正在使用spring framework + thymeleaf,很容易为你的项目添加转换器,通过简单创建一个从Formatter扩展的子类,然后在你的WebMvcConfigurerAdapter类中将你的类注册为bean并覆盖方法addFormatters( FormatterRegistry注册表)

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 公共类ConfiguracionWeb扩展WebMvcConfigurerAdapter实现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

相关问题