2015-10-21 69 views
3

我试图制作一张表格,显示来自数据库的信息,包括图像。问题是如何显示的图像: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) 

你能帮请?谢谢。

回答

7

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

试试这个:

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

谢谢,这解决了我的问题 – METTAIBI

2

我会推荐thymleafe URL语法添加任何数量的参数。

例如,

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

这将产生类似以下网址:
photoCat idCat =类别

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

它会生成URL类似以下内容:??
photoCat idCat =类别& q =查询

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

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