Skip to main content
 首页 » 编程设计

spring-boot之Thymeleaf:无法解析为表达式

2025年12月25日33jirigala

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

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

我用于显示图像的 Controller 方法:
@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) 

你能帮忙吗?谢谢你。

请您参考如下方法:

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

试试这个:

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