Error binding context to html template

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error binding context to html template

bigal.nz
Hi,

I am trying to get some form data into a html template and ultimately to PDF using iText. On the line:

String processedHtml = templateEngine.process(templateName, ctx);

I get the error:

2018-09-12 12:13:17.680 ERROR 18264 --- [nio-8080-exec-3] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-3] Exception processing template "output.html": An error happened during template parsing (template: "class path resource [templates/output.html]")

Caused by: org.attoparser.ParseException: Cannot process attribute '{th:field,data-th-field}': no associated BindStatus could be found for the intended form binding operations. This can be due to the lack of a proper management of the Spring RequestContext, which is usually done through the ThymeleafView or ThymeleafReactiveView (template: "output.html" - line 52, col 36)
        at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
        at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
        ... 61 common frames omitted

output.html line 52 is :

                            <input type="number" class="form-control inputnid person text-uppercase"
                                   data-property="nid" id="nia" placeholder="ID"
                                   th:field="*{assessment.nid}"/>

The full method is:

public class PdfGeneratorUtil {

    public static final String BASEURI = "src/main/resources/static";
    @Qualifier("templateEngine")
    @Autowired
    private TemplateEngine templateEngine;

    public void createPdf(String templateName, Map map) throws Exception {
        Assert.notNull(templateName, "The templateName can not be null");
        Context ctx = new Context();
        if (map != null) {
            Iterator itMap = map.entrySet().iterator();
            while (itMap.hasNext()) {
                Map.Entry pair = (Map.Entry) itMap.next();
                ctx.setVariable(pair.getKey().toString(), pair.getValue());
            }
        }

        String processedHtml = templateEngine.process(templateName, ctx);
        PdfWriter writer = new PdfWriter("C:\\tmp\\assessment.pdf");
        PdfDocument pdfDoc = new PdfDocument(writer);
        ConverterProperties converterProperties = new ConverterProperties().setBaseUri(BASEURI);
        HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
        System.out.println("PDF created successfully");
    }
}

Anyone know what is going on here?

Cheers

Al



Reply | Threaded
Open this post in threaded view
|

Re: Error binding context to html template

Emanuel
Administrator
I don't know too much about the error, but looking at the soure code where it happens (https://github.com/thymeleaf/thymeleaf-spring/blob/3.0-master/thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/processor/AbstractSpringFieldTagProcessor.java#L178-L180) it feels like at that point Thymeleaf is trying to figure out where to get "*{assessment.nid}" from, but can't.  The *{...} expression only works within the context of an outer <form th:object="${...}">, so maybe some things to check are:

 - is there a form object being declared on a <form> tag surrounding your <input> element?
 - does the form object have an assessment property (or getAssessment Java Bean method), which in turn has an nid object (or getNid Java Bean method)?
 - and then, as you enter your createPdf method, does the map being passed to it include something that would become the form object?
Reply | Threaded
Open this post in threaded view
|

Re: Error binding context to html template

bigal.nz
Silly me - turned out the fields were th:field and they should have been th:checked in this particular case.

Thanks Emanuel.

AG