代码之家  ›  专栏  ›  技术社区  ›  Al Grant

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

  •  1
  • Al Grant  · 技术社区  · 7 年前

    我正在尝试将一些表单数据转换成html模板,并最终使用itext转换成pdf格式。在线:

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

    我知道错误:

    2018-09-12 12 12:13:17.680错误18264---[NIO-8080-EXEC-3] 模板引擎: [thymeleaf][http-nio-8080-exec-3]异常处理模板 “output.html”:模板解析期间出错(模板: “类路径资源[templates/output.html]”)

    原因:org.attoparser.parseException:无法处理属性 “{th:field,data th field}”:找不到关联的bindstatus 用于预期的表单绑定操作。这可能是由于 对spring requestcontext的适当管理,通常是 通过ThymeleafView或ThymeleafReactiveView完成(模板: “output.html”-第52行,第36列) 在org.attopparser.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) ~【胸腺素-3.0.9.释放。jar:3.0.9.释放】 …省略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"); 
        } 
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Al Grant    7 年前

    找到了修复方法。正如堆栈跟踪在th:字段中暗示的那样,应该选中th:复选框。

    推荐文章