我已经在现有的Spring应用程序中实现了Flying-Saucer。模板使用Thymeleaf呈现,然后使用Tidy从HTML转换为xHTML。如果JQuery
内的值小于0,我想在模板内添加一些td
代码以将div
涂成红色。
我正在使用以下HTML结构:
<table class="dcf">
<tr>
<td>
<span>-500</span> €
</td>
<td>
<span>-500</span>
</td>
<td>
<span>-500</span>
</td>
</tr>
<tr>
<td>
<span>10.00,00</span>
</td>
</tr>
<tr>
<td>
<span>-5.000,23</span>
</td>
<td>
<span>5.000,23</span>
</td>
<td>
<span>5.000,23</span>
</td>
<td>
<span>5.000,23</span>
</td>
</tr>
</table>
和此JQuery代码:
$(document).ready(function() {
$(".dcf").find("tr").each(function() {
$(this).find("td").each(function() {
if (parseInt($(this).find("span").html()) < 0) {
$(this).css("color", "red");
}
})
});
})
这是一个jsFiddle http://jsfiddle.net/e51rynjx/2/
这是我的Java实现:
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(appContext);
templateResolver.setPrefix("classpath:/templates/pdf/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding(UTF_8);
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context context = new Context();
context.setVariables(data);
String renderedHtmlContent = templateEngine.process("bm001", context);
String xHtml = convertToXhtml(renderedHtmlContent);
ITextRenderer renderer = new ITextRenderer();
String baseUrl = FileSystems.getDefault().getPath("src", "main", "resources").toUri().toURL().toString();
renderer.setDocumentFromString(xHtml, baseUrl);
renderer.layout();
OutputStream outputStream = new FileOutputStream("test.pdf");
renderer.createPDF(outputStream);
outputStream.close();
为什么在将xHTML导出为PDF之前不执行JQuery?据我了解,HTML的呈现应由Thymeleaf处理。那么为什么JavaScript从未执行过?