我创建了两个thymleaf解析器并将它们添加到模板引擎中,因此我可以在spring-boot中处理txt / svg模板。
@Configuration
public class TemplateEngineConfig {
@Bean
public SpringTemplateEngine textTemplateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(textTemplateResolver());
templateEngine.addTemplateResolver(svgTemplateResolver());
return templateEngine;
}
private ITemplateResolver textTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(1);
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".txt");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCacheable(false);
return templateResolver;
}
private ITemplateResolver svgTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(2);
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".svg");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCacheable(false);
return templateResolver;
}
}
我这样使用它。
@Component
public class TemplateRenderer {
@Autowired
@Qualifier("textTemplateEngine")
private TemplateEngine textTemplateEngine;
public String renderPlainText(String filename, Map<String, Object> params) {
Context context = new Context(LocaleContextHolder.getLocale(), params);
return textTemplateEngine.process(filename, context);
}
}
它不起作用。我尝试渲染templates/img.svg
,但是Thymleaf抱怨它找不到templates/img.txt
文件,我怎么能让它工作?
答案 0 :(得分:2)
只需强制SpringTemplateEngine检查模板是否存在,而不仅仅是使用定义的模式。请强制添加
templateResolver.setCheckExistence(true);
到每个模板解析器。
答案 1 :(得分:1)
我认为你应该用@bean和Qualifier标记ITemplateResolver 你将@autowire textTemplateEngine或svgTemplateEngine,所以spring将使用限定符名称自动装配它们。
@Bean
@Qualifier("textTemplateEngine")
private ITemplateResolver textTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(1);
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".txt");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
@Qualifier("svgTemplateEngine")
private ITemplateResolver svgTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(2);
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".svg");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCacheable(false);
return templateResolver;
}
您也可以通过xml配置配置Thymeleaf多个解析器,如:
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8" />
<property name="templateEngine">
<bean class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="dialects">
<set>
<bean class="org.thymeleaf.spring4.dialect.SpringStandardDialect" />
</set>
</property>
<property name="templateResolvers">
<set>
<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="cacheable" value="false" />
<property name="prefix" value="/fragments/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="cacheable" value="false" />
<property name="prefix" value="/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
</set>
</property>
</bean>
</property>
</bean>