use of org.thymeleaf.TemplateEngine in project java-docs-samples by GoogleCloudPlatform.
the class MetadataServlet method init.
@Override
public void init() {
// Setup ThymeLeaf
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext());
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
// TTL=20m
templateResolver.setCacheTTLMs(Long.valueOf(1200000L));
// Cache is set to true by default. Set to false if you want templates to
// be automatically updated when modified.
templateResolver.setCacheable(true);
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
}
use of org.thymeleaf.TemplateEngine in project spring-boot by spring-projects.
the class ThymeleafServletAutoConfigurationTests method renderNonWebAppTemplate.
@Test
void renderNonWebAppTemplate() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(ThymeleafAutoConfiguration.class)).run((context) -> {
assertThat(context).doesNotHaveBean(ViewResolver.class);
TemplateEngine engine = context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("greeting", "Hello World"));
String result = engine.process("message", attrs);
assertThat(result).contains("Hello World");
});
}
use of org.thymeleaf.TemplateEngine in project spring-boot by spring-projects.
the class ThymeleafServletAutoConfigurationTests method useJava8TimeDialect.
@Test
void useJava8TimeDialect() {
this.contextRunner.run((context) -> {
TemplateEngine engine = context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK);
String result = engine.process("java8time-dialect", attrs).trim();
assertThat(result).isEqualTo("<html><body>2015-11-24</body></html>");
});
}
use of org.thymeleaf.TemplateEngine in project spring-boot by spring-projects.
the class ThymeleafServletAutoConfigurationTests method useSecurityDialect.
@Test
void useSecurityDialect() {
this.contextRunner.run((context) -> {
TemplateEngine engine = context.getBean(TemplateEngine.class);
MockServletContext servletContext = new MockServletContext();
JakartaServletWebApplication webApplication = JakartaServletWebApplication.buildApplication(servletContext);
WebContext attrs = new WebContext(webApplication.buildExchange(new MockHttpServletRequest(servletContext), new MockHttpServletResponse()));
try {
SecurityContextHolder.setContext(new SecurityContextImpl(new TestingAuthenticationToken("alice", "admin")));
String result = engine.process("security-dialect", attrs);
assertThat(result).isEqualTo("<html><body><div>alice</div></body></html>" + System.lineSeparator());
} finally {
SecurityContextHolder.clearContext();
}
});
}
use of org.thymeleaf.TemplateEngine in project spring-boot by spring-projects.
the class ThymeleafReactiveAutoConfigurationTests method createFromConfigClass.
@Test
void createFromConfigClass() {
this.contextRunner.withPropertyValues("spring.thymeleaf.suffix:.html").run((context) -> {
TemplateEngine engine = context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
String result = engine.process("template", attrs).trim();
assertThat(result).isEqualTo("<html>bar</html>");
});
}
Aggregations