Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package config;
- import org.thymeleaf.ITemplateEngine;
- import org.thymeleaf.TemplateEngine;
- import org.thymeleaf.templatemode.TemplateMode;
- import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
- import org.thymeleaf.web.IWebApplication;
- import org.thymeleaf.web.servlet.JakartaServletWebApplication;
- import jakarta.servlet.ServletContextEvent;
- import jakarta.servlet.ServletContextListener;
- import jakarta.servlet.annotation.WebListener;
- @WebListener
- public class ThymeleafConfig implements ServletContextListener {
- public static final String TEMPLATE_ENGINE_ATTR = "TemplateEngineInstance";
- private ITemplateEngine templateEngine;
- private JakartaServletWebApplication application;
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- this.application = JakartaServletWebApplication.buildApplication(sce.getServletContext());
- this.templateEngine = templateEngine(this.application);
- sce.getServletContext().setAttribute(TEMPLATE_ENGINE_ATTR, templateEngine);
- }
- private ITemplateEngine templateEngine(IWebApplication application) {
- TemplateEngine templateEngine = new TemplateEngine();
- WebApplicationTemplateResolver templateResolver = templateResolver(application);
- templateEngine.setTemplateResolver(templateResolver);
- return templateEngine;
- }
- private WebApplicationTemplateResolver templateResolver(IWebApplication application) {
- WebApplicationTemplateResolver templateResolver = new WebApplicationTemplateResolver(application);
- templateResolver.setTemplateMode(TemplateMode.HTML);
- templateResolver.setPrefix("/WEB-INF/templates/");
- templateResolver.setSuffix(".html");
- return templateResolver;
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement