【发布时间】:2015-07-01 04:57:05
【问题描述】:
这听起来像是一个微不足道的问题,但经过数小时的搜索,我还没有找到答案。据我了解,问题是我试图从控制器返回一个FileSystemResource,Thymeleaf 希望我提供一个String 资源,它将使用它来呈现下一页。但由于我返回的是FileSystemResource,因此出现以下错误:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "products/download", template might not exist or might not be accessible by any of the configured Template Resolvers
我使用的控制器映射是:
@RequestMapping(value="/products/download", method=RequestMethod.GET)
public FileSystemResource downloadFile(@Param(value="id") Long id) {
Product product = productRepo.findOne(id);
return new FileSystemResource(new File(product.getFileUrl()));
}
我的 HTML 链接如下所示:
<a th:href="${'products/download?id=' + product.id}"><span th:text="${product.name}"></span></a>
我不想被重定向到任何地方,我只需要在单击链接后下载文件。这实际上是正确的实现吗?我不确定。
【问题讨论】:
-
<a th:href="${'products/download?id=' + product.id}"><span th:text="${product.name}"></span></a>开头的$应该是@,你可以试试吗? -
如果我使用
@,product.id不会被实际产品 ID 替换。 -
因为它应该是
th:href="@{'products/download?id=' + ${product.id}}",如documentation中所述 -
是的。但错误仍然存在。实际 id 现在显示在 get 请求中。
-
@PatrickLC 成功了!如果你把它作为答案,我会接受它。
标签: java download spring-boot thymeleaf