Reputation: 1090
I'm trying to resolve a thymeleaf template but, as the return of the process() method, I'm getting the name of the template. I would like to get the raw html code to store it in the data layer for future use.
@Override
public String process(String template, Map<String, Object> model) {
String process = templateEngine.process(template, new Context(config.getLocale(), model));
return process;
}
I've tried to use all of the resolvers but they all work the same way. Is there any way to do this without reading the html file as a string and parsing for target pattern?
Upvotes: 0
Views: 750
Reputation: 2491
You can have a template configured as RAW (More info about Template Mode).
For example in a @Configuration
class define this:
@Configuration
public class ThymeleafConfig {
@Bean(name = "rawTemplateEngine")
public TemplateEngine rawTemplateEngine() {
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.addTemplateResolver(rawTemplateResolver());
return templateEngine;
}
private ITemplateResolver rawTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("/templates/"); //or whatever other directory you have the files
templateResolver.setSuffix(".html"); //if they are html files
templateResolver.setTemplateMode(TemplateMode.RAW);
templateResolver.setForceTemplateMode(true); //to turn off suffix-based choosing
templateResolver.setCharacterEncoding("UTF8");
templateResolver.setCheckExistence(true);
return templateResolver;
}
}
Please note, without setForceTemplateMode
the smart template mode resolution overrides setTemplateMode
(Info about smart template resolution).
Then, in the class that you need this bean, autowire it:
@Autowired
@Qualifier("rawTemplateEngine")
TemplateEngine templateEngineRaw;
And then call:
String process = templateEngineRaw.process(template, new Context(config.getLocale()));
Upvotes: 2