Search in sources :

Example 6 with SpringTemplateEngine

use of org.thymeleaf.spring5.SpringTemplateEngine in project thymeleaf-tests by thymeleaf.

the class TemplateEngineTest method testTemplateResolverConfiguration06.

@Test
public void testTemplateResolverConfiguration06() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver());
    templateEngine.addTemplateResolver(new FileTemplateResolver());
    templateEngine.initialize();
    final List<ITemplateResolver> templateResolvers = new ArrayList<ITemplateResolver>(templateEngine.getTemplateResolvers());
    Assert.assertEquals(2, templateResolvers.size());
    Assert.assertEquals("org.thymeleaf.templateresolver.ClassLoaderTemplateResolver", templateResolvers.get(0).getName());
    Assert.assertEquals("org.thymeleaf.templateresolver.FileTemplateResolver", templateResolvers.get(1).getName());
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) ArrayList(java.util.ArrayList) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) Test(org.junit.Test)

Example 7 with SpringTemplateEngine

use of org.thymeleaf.spring5.SpringTemplateEngine in project thymeleaf-tests by thymeleaf.

the class TemplateEngineTest method testDefaultTemplateResolver04.

@Test
public void testDefaultTemplateResolver04() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    final Context context = new Context();
    context.setLocale(Locale.ENGLISH);
    context.setVariable("one", "this value");
    final ClassLoaderTemplateResolver classLoaderTemplateResolver = new ClassLoaderTemplateResolver();
    classLoaderTemplateResolver.setCheckExistence(true);
    templateEngine.addTemplateResolver(classLoaderTemplateResolver);
    final DefaultTemplateResolver defaultTemplateResolver = new DefaultTemplateResolver();
    defaultTemplateResolver.setTemplate("<p th:text=\"${one}\">something</p>");
    templateEngine.addTemplateResolver(defaultTemplateResolver);
    templateEngine.initialize();
    final List<ITemplateResolver> templateResolvers = new ArrayList<ITemplateResolver>(templateEngine.getTemplateResolvers());
    Assert.assertEquals(2, templateResolvers.size());
    Assert.assertEquals("org.thymeleaf.templateresolver.ClassLoaderTemplateResolver", templateResolvers.get(0).getName());
    Assert.assertEquals("org.thymeleaf.templateresolver.DefaultTemplateResolver", templateResolvers.get(1).getName());
    Assert.assertEquals("<p>this value</p>", templateEngine.process("nonexisting", context));
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) ArrayList(java.util.ArrayList) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) Test(org.junit.Test)

Example 8 with SpringTemplateEngine

use of org.thymeleaf.spring5.SpringTemplateEngine in project thymeleaf-tests by thymeleaf.

the class SpringResourceTemplateResolverSpring4Test method testResolveTemplate.

@Test
public void testResolveTemplate() throws Exception {
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    final IEngineConfiguration configuration = templateEngine.getConfiguration();
    final String templateLocation = "spring5/templateresolver/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring5/templateresolver/applicationContext.xml");
    final SpringResourceTemplateResolver resolver = (SpringResourceTemplateResolver) context.getBean("springResourceTemplateResolver");
    final TemplateMode templateMode = resolver.getTemplateMode();
    Assert.assertEquals(TemplateMode.HTML, templateMode);
    final TemplateResolution resolution = resolver.resolveTemplate(configuration, null, "classpath:" + templateLocation, null);
    final ITemplateResource templateResource = resolution.getTemplateResource();
    final String templateResourceStr = IOUtils.toString(templateResource.reader());
    final String testResource = templateResourceStr.replace("\r", "");
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(SpringResourceTemplateResolverSpring4Test.class).getResourceAsStream(templateLocation), "US-ASCII", true).replace("\r", "");
    Assert.assertEquals(expected, testResource);
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateMode(org.thymeleaf.templatemode.TemplateMode) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) Test(org.junit.Test)

Example 9 with SpringTemplateEngine

use of org.thymeleaf.spring5.SpringTemplateEngine in project cas by apereo.

the class CasThemesConfiguration method nonCachingThymeleafViewResolver.

private ThymeleafViewResolver nonCachingThymeleafViewResolver() {
    final ThymeleafViewResolver r = new ThymeleafViewResolver();
    r.setAlwaysProcessRedirectAndForward(this.thymeleafViewResolver.getAlwaysProcessRedirectAndForward());
    r.setApplicationContext(this.thymeleafViewResolver.getApplicationContext());
    r.setCacheUnresolved(this.thymeleafViewResolver.isCacheUnresolved());
    r.setCharacterEncoding(this.thymeleafViewResolver.getCharacterEncoding());
    r.setContentType(this.thymeleafViewResolver.getContentType());
    r.setExcludedViewNames(this.thymeleafViewResolver.getExcludedViewNames());
    r.setOrder(this.thymeleafViewResolver.getOrder());
    r.setRedirectContextRelative(this.thymeleafViewResolver.isRedirectContextRelative());
    r.setRedirectHttp10Compatible(this.thymeleafViewResolver.isRedirectHttp10Compatible());
    r.setStaticVariables(this.thymeleafViewResolver.getStaticVariables());
    r.setForceContentType(this.thymeleafViewResolver.getForceContentType());
    final SpringTemplateEngine engine = SpringTemplateEngine.class.cast(this.thymeleafViewResolver.getTemplateEngine());
    engine.addDialect(new IPostProcessorDialect() {

        @Override
        public int getDialectPostProcessorPrecedence() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Set<IPostProcessor> getPostProcessors() {
            return CollectionUtils.wrapSet(new PostProcessor(TemplateMode.parse(thymeleafProperties.getMode()), CasThymeleafOutputTemplateHandler.class, Integer.MAX_VALUE));
        }

        @Override
        public String getName() {
            return CasThymeleafOutputTemplateHandler.class.getSimpleName();
        }
    });
    r.setTemplateEngine(engine);
    r.setViewNames(this.thymeleafViewResolver.getViewNames());
    // disable the cache
    r.setCache(false);
    // return this ViewResolver
    return r;
}
Also used : SpringTemplateEngine(org.thymeleaf.spring4.SpringTemplateEngine) Set(java.util.Set) ThymeleafViewResolver(org.thymeleaf.spring4.view.ThymeleafViewResolver) IPostProcessorDialect(org.thymeleaf.dialect.IPostProcessorDialect) CasThymeleafOutputTemplateHandler(org.apereo.cas.services.web.CasThymeleafOutputTemplateHandler) PostProcessor(org.thymeleaf.postprocessor.PostProcessor) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor)

Example 10 with SpringTemplateEngine

use of org.thymeleaf.spring5.SpringTemplateEngine in project yqq by yeycfri.

the class WebConfig method templateEngine.

@Bean
public TemplateEngine templateEngine(TemplateResolver templateResolver) {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    return templateEngine;
}
Also used : SpringTemplateEngine(org.thymeleaf.spring4.SpringTemplateEngine) Bean(org.springframework.context.annotation.Bean)

Aggregations

SpringTemplateEngine (org.thymeleaf.spring4.SpringTemplateEngine)15 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)13 Test (org.junit.Test)11 Bean (org.springframework.context.annotation.Bean)11 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)10 ArrayList (java.util.ArrayList)7 Context (org.thymeleaf.context.Context)6 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)6 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)3 FileTemplateResolver (org.thymeleaf.templateresolver.FileTemplateResolver)3 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)3 TemplateEngine (org.thymeleaf.TemplateEngine)2 CustomDialect (com.baeldung.dialect.CustomDialect)1 InstanceRepository (de.codecentric.boot.admin.server.domain.entities.InstanceRepository)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Properties (java.util.Properties)1