Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何注册SpringTemplateEngine的转换服务?_Java_Spring_Thymeleaf - Fatal编程技术网

Java 如何注册SpringTemplateEngine的转换服务?

Java 如何注册SpringTemplateEngine的转换服务?,java,spring,thymeleaf,Java,Spring,Thymeleaf,我有一个控制台应用程序,它使用thymeleaf生成电子邮件模板 据我所知,只有spring模板引擎能够利用转换服务对thymeleaf上下文变量应用全局格式 如何在spring模板引擎中注册转换服务 // init the template engine templateEngine = new SpringTemplateEngine(); templateResolver = new ClassLoaderTemplateResolver(); templateEngine.addTemp

我有一个控制台应用程序,它使用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);
由于转换器接口是spring的一部分,因此仍然需要thymeleaf spring依赖项:

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

org.thymeleaf
百里香
2.1.4.1发布

如果您使用的是spring framework+thymeleaf,只需创建一个从Formatter扩展的子类,然后在WebMVCConfigureAdapter类中将您的类注册为bean,并重写addFormatters方法(FormatterRegistry registry registry)

@配置 公共类ConfigurationWeb扩展WebMVCConfigureAdapter实现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}

你可以看到,这就是文件

不要自己构造
SpringTemplateEngine
ClassLoaderTemplateResolver
,让Spring这样做,然后简单地将它注入到bean中。要使用转换,如果我没有弄错的话,您必须使用Spring方言来实现这一点。我有同样的问题-也使用Thymeleaf在标准Thymeleaf上下文旁边发送邮件,用于常规WebMVC内容。奇怪的是,这两种Thymeleaf配置似乎拥有不同的转换服务。WebMVC中的一个添加了大量有用的东西,比如ZoneDateTime->String,我在邮件上下文中无法访问这些东西。。。很遗憾,您没有找到使用标准转换服务的解决方案。。。
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>
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
@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();
}