我试图获得一些表单数据到一个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异常处理模板"output.html":模板解析时出错(模板:"class path resource templates/output.html")
原因: org.attoparser.ParseException:无法处理属性“{th: BindStatus,data-th-field}”:找不到与所需窗体绑定操作相关联的字段。这可能是由于缺乏对Spring的适当管理,这通常是通过ThymeleafView或ThymeleafReactiveView完成的( RequestContext:"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) ~胸腺叶-3.0.9.RELEASE.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");
}
}
发布于 2018-09-12 09:47:41
找到修复方法了。正如堆栈跟踪在th:字段提示的那样,复选框应为th:选中。
https://stackoverflow.com/questions/52285833
复制相似问题