我要从一个受Spring Security保护的管理部分的Web应用程序中下载一个PDF文件,该Web应用程序是由Spring Boot 2.0.3开发并使用Thymeleaf开发的。 在本地运行正常,但在网上出现此错误:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/email/confirmedbooking", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
这是控制器:
@GetMapping("/admin/bookings/booking-pdf")
public void generatePdfBooking(@RequestParam Long idbooking, HttpServletResponse response)
throws IOException, Exception{
bookingService.setBookingService(idbooking);
Booking booking = bookingService.getBooking();
Guest guest = bookingService.getGuest();
String idlanguage;
if(guest.getIdlanguage() != null){
idlanguage = guest.getIdlanguage();
} else {idlanguage = "en";}
Map<String, Object> map = new HashMap<>();
map.put("booking", booking);
map.put("guest", guest);
byte[] data = pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);
pdfGenerator.streamReport(response, data, "id-" + booking.getIdbooking() + ".pdf");
}
这是html页面的一部分:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="/webjars/bootstrap /css/bootstrap.min.css"/>
<link rel="stylesheet" href="/css/rentalwebs.css"/>
</head>
<body>
<table style="width:680px" class="table table-borderless">
<tbody>
<tr>
<td>
<h2 th:text="${property.name}"></h2>
</td>
</tr>
<tr>
<td>
<span th:text="#{booking.id}"></span>
<span th:text="${booking.idbooking}"></span>
....
作为模板生成器,我使用的是org.xhtmlrenderer.pdf.ITextRenderer(flying-saucer-pdf)。
我已经测试过提供不同的.html文件以在pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);
处生成PDF,但是结果始终相同。
答案 0 :(得分:0)
按照丹尼尔·米库萨(Daniel Mikusa)的建议,我能够解决问题,从/email/confirmedbooking
中取出第一个/,留下这样的代码:
byte[] data = pdfGenerator.createPdf("email/confirmedbooking", map, idlanguage);
似乎似乎无法识别路径,可能是由于双斜杠问题所致,已在本帖子中进行了解释:Error resolving template with Spring Boot using Thymeleaf packaged in a jar