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

问题描述 投票:1回答:1

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

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

我收到错误:

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

由以下原因引起:org.attoparser.ParseException:无法处理属性'{th:field,data-th-field}':找不到与预期的表单绑定操作相关联的BindStatus。这可能是由于缺乏对Spring RequestContext的正确管理,这通常是通过org.attoparser.MarkupParser.parseDocument(MarkupParser)中的ThymeleafView或ThymeleafReactiveView(模板:“output.html” - 第52行,第36栏)完成的。 .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] at 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}"/>

完整的方法是:

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"); 
    } 
}
java html spring-boot itext thymeleaf
1个回答
1
投票

找到了解决方案。由于在:字段中暗示的堆栈跟踪应该是:选中复选框。

© www.soinside.com 2019 - 2024. All rights reserved.