Search in sources :

Example 21 with TemplateEngine

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);
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) ServletContextTemplateResolver(org.thymeleaf.templateresolver.ServletContextTemplateResolver)

Example 22 with TemplateEngine

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");
    });
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebContext(org.thymeleaf.context.WebContext) ServletContext(jakarta.servlet.ServletContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) Test(org.junit.jupiter.api.Test)

Example 23 with TemplateEngine

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>");
    });
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebContext(org.thymeleaf.context.WebContext) ServletContext(jakarta.servlet.ServletContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) Test(org.junit.jupiter.api.Test)

Example 24 with TemplateEngine

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();
        }
    });
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) JakartaServletWebApplication(org.thymeleaf.web.servlet.JakartaServletWebApplication) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) WebContext(org.thymeleaf.context.WebContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 25 with TemplateEngine

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>");
    });
}
Also used : WebContext(org.thymeleaf.context.WebContext) Context(org.thymeleaf.context.Context) ISpringWebFluxTemplateEngine(org.thymeleaf.spring6.ISpringWebFluxTemplateEngine) SpringWebFluxTemplateEngine(org.thymeleaf.spring6.SpringWebFluxTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) Test(org.junit.jupiter.api.Test)

Aggregations

TemplateEngine (org.thymeleaf.TemplateEngine)49 Context (org.thymeleaf.context.Context)28 Test (org.junit.Test)17 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)13 MockServletContext (org.springframework.mock.web.MockServletContext)11 RequestContext (org.springframework.web.servlet.support.RequestContext)10 Test (org.junit.jupiter.api.Test)7 WebContext (org.thymeleaf.context.WebContext)7 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 SpringTemplateEngine (org.thymeleaf.spring6.SpringTemplateEngine)6 ServletContext (jakarta.servlet.ServletContext)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)5 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)5 ServletContextTemplateResolver (org.thymeleaf.templateresolver.ServletContextTemplateResolver)4 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)3 TemplateSpec (org.thymeleaf.TemplateSpec)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2