Thymeleaf:无法解析为表达式

MET*_*IBI 7 thymeleaf spring-boot

我正在尝试制作一个表格,显示数据库中的信息,包括图像.问题是如何显示图像:

<table>
    <tr>
        <th>ID Catégorie:</th>
        <th>Nom:</th>
        <th>Description:</th>
        <th>Photo:</th>
    </tr>
    <tr th:each="cat : ${categories}">
        <td><img th:src="photoCat?idCat=${cat.idCategorie}"/></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我的控制器显示图像的方法:

@RequestMapping(value="photoCat", produces=MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] photoCat(Long idCat) throws IOException {
    Categorie c = adminCatService.getCategorie(idCat);
    return org.apache.commons.io.IOUtils.toByteArray(new ByteArrayInputStream(c.getPhoto()));
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "photoCat?idCat=${cat.idCategorie}" (categories:53)
Run Code Online (Sandbox Code Playgroud)

你能帮帮忙吗?谢谢.

lyd*_*ric 11

你不能在Thymeleaf中嵌入这样的变量.

试试这个:

<img th:src="${'photoCat?idCat=' + cat.idCategorie}" />
Run Code Online (Sandbox Code Playgroud)


Piy*_*ush 6

我建议使用 thymleafe url 语法添加任意数量的参数。

例如,

<img th:src="@{/photoCat(idCat=${cat.idCategorie}}" />
Run Code Online (Sandbox Code Playgroud)

它会生成如下 URL:
photoCat?idCat=category

<img th:src="@{/photoCat(idCat=${cat.idCategorie},q=${query}}" />
Run Code Online (Sandbox Code Playgroud)

它将生成如下 URL:
photoCat?idCat=category&q=query

它还将为您对所有变量进行 URI 编码。

检查文档以获取更多示例。
http://www.thymeleaf.org/doc/articles/standardurlsyntax.html