Thymeleaf:无法解析为表达式

Posted

技术标签:

【中文标题】Thymeleaf:无法解析为表达式【英文标题】:Thymeleaf: Could not parse as expression 【发布时间】:2016-01-21 03:03:42 【问题描述】:

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

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

你能帮忙吗?谢谢。

【问题讨论】:

【参考方案1】:

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

试试这个:

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

【讨论】:

【参考方案2】:

我会推荐 thymleafe url 语法来添加任意数量的参数。

例如,

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

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

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

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

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

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

【讨论】:

【参考方案3】:

你必须像这样放置图像源网址

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

【讨论】:

以上是关于Thymeleaf:无法解析为表达式的主要内容,如果未能解决你的问题,请参考以下文章