Thymeleaf:无法解析为表达式

时间:2015-10-21 23:11:01

标签: spring-boot thymeleaf

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

<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>

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

@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()));
}

我收到以下错误:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "photoCat?idCat=${cat.idCategorie}" (categories:53)
你能帮忙吗?谢谢。

3 个答案:

答案 0 :(得分:10)

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

请改为尝试:

<img th:src="${'photoCat?idCat=' + cat.idCategorie}" />

答案 1 :(得分:2)

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

例如,

<img th:src="@{/photoCat(idCat=${cat.idCategorie}}" />

它将生成以下URL:
photoCat?idCat =类别

<img th:src="@{/photoCat(idCat=${cat.idCategorie},q=${query}}" />

它将生成以下URL:
photoCat idCat =类别&安培; Q =查询

它还将为您编码所有变量的URI。

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

答案 2 :(得分:0)

您必须像这样放置图片源网址

<img th :src="${image soruce url}" />