0

I can see there are a few question already asked in there, but still I can't find any solution to fix my issue. I am using Spring boot Thymeleafs Template Engine to parse value attribute with Spring Context. Then, I can process PDF generated with flying-saucer-pdf.

I can say the problem is because String is passed to parameter and it is not referent to html file. Error description:

2022-10-11T16:45:58.209+0700 ERROR [THYMELEAF][http-nio-8080-exec-2] Exception processing template "world-check-report": An error happened during template parsing (template: "applicationprocess/world-check-report.html")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "applicationprocess/world-check-report.html")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1059) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1048) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at com.aeon.webadmin.controller.AssessmentController.getWorldCheckStatus(AssessmentController.java:820) [classes/:1.3.5]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_333]
Caused by: java.io.FileNotFoundException: ClassLoader resource "applicationprocess/world-check-report.html" could not be resolved
at org.thymeleaf.templateresource.ClassLoaderTemplateResource.reader(ClassLoaderTemplateResource.java:130) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]

My Controller:

@PostMapping(value = "/world-check-info")
public ObjResponseEntity<AmlApplyLoan> getWorldCheckStatus(@RequestBody String aln_id) {
    try {
        HttpHeaders headers = new HttpHeaders();
        AssessmentController assessmentController = new AssessmentController();
        
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("applicationprocess/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setCharacterEncoding("UTF-8");

        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

        Context ctx = new Context();
        ctx.setVariable("message", "TESTING");
        
        //error here when parsing 
        String htmtContent = templateEngine.process("world-check-report", ctx);
        
        return ObjResponseFactory.success();
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ObjResponseFactory.fail();

Template File:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
<body>
    <div class="content-wrapper" style="padding-left: 120px; font-family: sans-serif;">
        <h2 class="page_title" th:text="${message}"></h2>
        <h2>CASE REPORT</h2>    
    </div>
</body>
</html>
11Strong
  • 129
  • 6
  • I'm trying to make a configuration class but I still getting another error which is all the html files are applied with Template Engine. Actually, there is only a file that used ClassLoaderTemplateResolver and Template Engine parsed. – 11Strong Oct 11 '22 at 10:07
  • Can you add also the template file? – artiomi Oct 11 '22 at 10:35
  • Hi @artiomi, template files is simple here:

    – 11Strong Oct 11 '22 at 10:40
  • There is no problem with your use of `TemplateEngine` and `Context`, what is the absolute path to `applicationprocess` folder? – İsmail Y. Oct 11 '22 at 10:57
  • @İsmailY. inside applicationprocess folder, there is html file that I use to write data template file: world-check-report. templateEngine.process("world-check-report", ctx); – 11Strong Oct 12 '22 at 01:28
  • 1
    Your templates will be picked up automatically from `src/main/resources/applicationprocess`. What is the absolute path to the `applicationprocess` folder? So your folder is here? – İsmail Y. Oct 12 '22 at 07:56
  • Dear @İsmailY, there is too long to get back to you. I already solved the issue by manual create new class to receive data from back-end and pass to Thymleaf. Then I can execute PDF generated. Thank you – 11Strong Apr 04 '23 at 09:03

0 Answers0