我想在我的Spring Boot应用程序中使用百里香模板。我在src/resources/public
目录中有一些html页面,并且我希望在不编写控制器的情况下就可以使用它们,因为我想编写一个前端应用程序。我可以通过index.html
访问http://localhost:8080/index.html
文件,但是它没有使用thymeleaf语法。我该怎么办?
我添加了thymeleaf依赖项,尝试编写SpringResourceTemplateResolver
bean,但仍然没有运气。到目前为止,这是我所做的:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public SpringResourceTemplateResolver templateResolver(){
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/public/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(true);
return templateResolver;
}
}
答案 0 :(得分:0)
您确实可以在Spring Boot应用程序的控制器上下文之外使用Thymeleaf。这种用法的一个例子是生成电子邮件文本。
我建议在控制器外部使用Thymeleaf的方法是创建一个单独的配置类,该类将Thymeleaf的DE_DE
的bean实例作为实例加载,例如:
TemplateResolver
然后,您可以使用以下代码将模板解析器注入到Spring服务,组件等中:
...
import org.springframework.context.annotation.*;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
public class ThymeleafConfiguration {
@SuppressWarnings("unused")
private final Logger log = LoggerFactory.getLogger(ThymeleafConfiguration.class);
@Bean("mailTemplate")
@Description("Thymeleaf template resolver serving HTML 5 emails")
public ClassLoaderTemplateResolver emailTemplateResolver() {
// This configuration assumes that for this resolver the Thymeleaf templates
// will be in the src/main/resources/mail of your project.
return createTemplateResolver("mails/", ".html", "HTML5");
}
@Bean("htmlTemplate")
@Description("Thymeleaf template resolver serving HTML 5 which is directly sent out to the user")
public ClassLoaderTemplateResolver htmlTemplateResolver() {
// This configuration assumes that for this resolver the Thymeleaf templates
// will be in the src/main/resources/html of your project.
return createTemplateResolver("html/", ".html", "HTML5");
}
@Bean(value = "xmlTemplate")
ClassLoaderTemplateResolver xmlTemplateResolver() {
return createTemplateResolver("xmltemplates/", ".xml", "XML");
}
private ClassLoaderTemplateResolver createTemplateResolver(String prefix, String suffix, String html5) {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix(prefix);
emailTemplateResolver.setSuffix(suffix);
emailTemplateResolver.setTemplateMode(html5);
emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
emailTemplateResolver.setOrder(1);
return emailTemplateResolver;
}
}
然后您可以简单地使用引擎:
@Qualifier("mailTemplate")
private final SpringTemplateEngine templateEngine;
@Qualifier("xmlTemplate")
private final SpringTemplateEngine xmlTemplate;
上面的代码已在带有Spring Boot 1.5.10.RELEASE的项目中使用
Maven Thymeleaf依赖项是:
private String renderInEngine(Visitor visitor, List<Travel> travelDTOS,
SpringTemplateEngine templateEngine, String template) {
Context context = new Context(ENGLISH);
context.setVariable(VISITOR_PARAM, visitor);
context.setVariable(TRAVEL_DETAILS_PARAM, travelDTOS);
context.setVariable(CONSENT_RECEIVED_BASE_URL, consentReceivedBaseURL);
context.setVariable(ENCRYPTED_USER_ID, simpleEncryptionService.encrypt(Long.toString(visitor.getId())));
return templateEngine.process(template, context);
}