Process HTML file using Thymeleaf in Web based Scopes of Spring and store the processed template as String

后端 未结 5 1487
无人共我
无人共我 2021-01-02 14:21

I am trying to render an HTML file using thymeleaf and keep the resultant HTML content in a String variable in web-based scopes of Spring so that i can use it l

5条回答
  •  误落风尘
    2021-01-02 14:57

    If TemplateEngine is autowired inside a singleton bean, then the below mentioned code works perfect.

    @Controller
    public class jataController {
    
        @Autowired
        private TemplateEngine templateEngine;
    
        @GetMapping(value = "/manual-thym")
        @ResponseBody
        public void justSample() {
            Context context = new Context();
            String filename = "templates/view/generated-ticket.html";
            String html = renderHtml(filename, context);
            System.out.println("template\n" + html);
        }
    
        private String renderHtml(String filename, Context context) {
    
            ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
            templateResolver.setSuffix(".html");
            templateResolver.setTemplateMode(TemplateMode.HTML);
            templateResolver.setCacheable(false);
            templateResolver.setOrder(1);
            templateResolver.setCharacterEncoding("UTF-8");
    
            templateEngine.setTemplateResolver(templateResolver);
    
            String html = templateEngine.process(filename, context);
    
            return html;
        }
    }
    

    but if TemplateEngine is autowired on a request scope bean type, it gives exception and thymeleaf will create a memory leak. So finally with lots of hit and tries, i got a working solution and thanks to @Paizo. It might contain some bad practice but this is how it worked:

    @Controller
    @Configuration
    @EnableWebMvc
    @ApplicationScope
    public class MyThymeleafConfig {
    
        @GetMapping("/view-template")
        @ResponseBody
        public void viewTemplates() {
    
            Context context = new Context();
            context.setVariable("mydata", "this is it");
    
            String html = templateEngine().process("templates/view-to-process.html", context);
            System.out.println(html);
        }
    
    
        /*
    
        configuration for thymeleaf and template processing
    
        */
    
        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(thymeleafTemplateResolver());
            return templateEngine;
        }
    
        @Bean
        public SpringResourceTemplateResolver thymeleafTemplateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setPrefix("classpath:");
            templateResolver.setSuffix(".html");
            templateResolver.setCacheable(false);
            templateResolver.setTemplateMode(TemplateMode.HTML);
            return templateResolver;
        }
    
        @Bean
        public ThymeleafViewResolver thymeleafViewResolver() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            return viewResolver;
        }
    }
    

    and to serve the static resources we need to define another bean, which is as follows:

    @Configuration
    @EnableWebMvc
    public class StaticResourceConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
                    .addResourceHandler("/**")
                    .addResourceLocations("/static/", "classpath:static/");
        }
    }
    

    This solution cannot be found on any forum. However i would ask as well as request any Spring developer to provide a better implementation for the above code.

提交回复
热议问题