Search in sources :

Example 1 with SpringTemplateEngine

use of org.thymeleaf.spring6.SpringTemplateEngine in project spring-security by spring-projects.

the class WebMvcConfiguration method templateEngine.

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

Example 2 with SpringTemplateEngine

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

the class LinkBuilderTest method testLinkBuilderSpringWithECFactory01.

@Test
public void testLinkBuilderSpringWithECFactory01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(new StringTemplateResolver());
    templateEngine.setLinkBuilder(new TestLinkBuilder());
    templateEngine.setEngineContextFactory(new TestEngineContextFactory());
    final String result = templateEngine.process("<a th:href='@{/something}'>", ctx);
    final String expected = "<a href='[ENGINE: /fromthebuilder/something]'>";
    Assert.assertEquals(expected, result);
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 3 with SpringTemplateEngine

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

the class DecoupledSTSMTest method test.

private static void test(final String templateName, final String expectedResultLocation, final Map<String, Object> vars) throws Exception {
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, Locale.US);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
    appCtx.setServletContext(mockServletContext);
    appCtx.setConfigLocation("classpath:templateparser/decoupled/stsm/applicationContext.xml");
    appCtx.refresh();
    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    // can be null!
    final ConversionService conversionService = getConversionService(appCtx);
    mockRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    final ServletWebRequest servletWebRequest = new ServletWebRequest(mockRequest, mockResponse);
    RequestContextHolder.setRequestAttributes(servletWebRequest);
    final RequestContext requestContext = new RequestContext(mockRequest, mockResponse, mockServletContext, vars);
    vars.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext);
    vars.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext);
    vars.put(SpringContextVariableNames.THYMELEAF_REQUEST_CONTEXT, new SpringWebMvcThymeleafRequestContext(requestContext, mockRequest));
    final List<String> variableNames = new ArrayList<String>(vars.keySet());
    for (final String variableName : variableNames) {
        final Object bindingObject = vars.get(variableName);
        if (isBindingCandidate(variableName, bindingObject)) {
            final String bindingVariableName = BindingResult.MODEL_KEY_PREFIX + variableName;
            if (!vars.containsKey(bindingVariableName)) {
                final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName);
                dataBinder.setConversionService(conversionService);
                vars.put(bindingVariableName, dataBinder.getBindingResult());
            }
        }
    }
    final WebContext ctx = new WebContext(mockRequest, mockResponse, mockServletContext, Locale.US);
    for (final Map.Entry<String, Object> entry : vars.entrySet()) {
        ctx.setVariable(entry.getKey(), entry.getValue());
    }
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templateparser/decoupled/stsm/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setUseDecoupledLogic(true);
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setLinkBuilder(new StandardLinkBuilder() {

        @Override
        protected String computeContextPath(final IExpressionContext context, final String base, final Map<String, Object> parameters) {
            return "";
        }
    });
    appCtx.getAutowireCapableBeanFactory().initializeBean(templateEngine, "templateEngine");
    final String result = templateEngine.process(templateName, ctx);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(DecoupledSTSMTest.class).getResourceAsStream(expectedResultLocation), "ISO-8859-1", true);
    Assert.assertEquals(ResourceUtils.normalize(expected), ResourceUtils.normalize(result));
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) WebContext(org.thymeleaf.context.WebContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDataBinder(org.springframework.web.bind.WebDataBinder) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) IExpressionContext(org.thymeleaf.context.IExpressionContext) ConversionService(org.springframework.core.convert.ConversionService) ServletContext(javax.servlet.ServletContext) RequestContext(org.springframework.web.servlet.support.RequestContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with SpringTemplateEngine

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

the class TemplateEngineTest method testDefaultTemplateResolver02.

@Test
public void testDefaultTemplateResolver02() {
    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>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>something</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 5 with SpringTemplateEngine

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

the class TemplateEngineTest method testTemplateResolverConfiguration08.

@Test
public void testTemplateResolverConfiguration08() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(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)

Aggregations

SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)15 SpringTemplateEngine (org.thymeleaf.spring4.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