Java 使用thymeleaf将上下文绑定到html模板时出错

Java 使用thymeleaf将上下文绑定到html模板时出错,java,html,spring-boot,itext,thymeleaf,Java,Html,Spring Boot,Itext,Thymeleaf,我试图将一些表单数据转换成html模板,并最终使用iText转换成PDF。在线: String processedHtml = templateEngine.process(templateName, ctx); 我得到一个错误: 2018-09-12 12:13:17.680错误18264---[nio-8080-exec-3] org.thymeleaf.TemplateEngine: [THYMELEAF][http-nio-8080-exec-3]异常处理模板 “output.html

我试图将一些表单数据转换成html模板,并最终使用iText转换成PDF。在线:

String processedHtml = templateEngine.process(templateName, ctx);
我得到一个错误:

2018-09-12 12:13:17.680错误18264---[nio-8080-exec-3] org.thymeleaf.TemplateEngine: [THYMELEAF][http-nio-8080-exec-3]异常处理模板 “output.html”:模板解析过程中发生错误(模板: “类路径资源[templates/output.html]”)

原因:org.attoparser.ParseException:无法处理属性 “{th:field,data th field}”:找不到关联的BindStatus 用于预期的表单绑定操作。这可能是由于缺乏 对SpringRequestContext进行适当的管理,这通常是 通过ThymileAfView或ThymileAfreactiveView完成(模板: “output.html”-第52行,第36列) 位于org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE] 在org.attoparser.MarkupParser.parse(MarkupParser.java:257)~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE] 位于org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] ... 省略了61个公共帧

output.html第52行是:

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

找到了解决办法。正如在th:字段中暗示的堆栈跟踪,应选中th:复选框

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"); 
    } 
}