I'm trying to make a table showing information from database including an image. The problem is how to show the image:
<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>
My controller method for showing the image:
@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()));
}
I'm getting the following error:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "photoCat?idCat=${cat.idCategorie}" (categories:53)
Can you help please? Thank you.
You can't embed variables like this in Thymeleaf.
Try this instead:
<img th:src="${'photoCat?idCat=' + cat.idCategorie}" />
I would recommand thymleafe url syntax to add any number of parameter.
For example,
<img th:src="@{/photoCat(idCat=${cat.idCategorie}}" />
It would generate URL like following:
photoCat?idCat=category
<img th:src="@{/photoCat(idCat=${cat.idCategorie},q=${query}}" />
It would generate URL like following:
photoCat?idCat=category&q=query
It will also URI encode all the variables for you.
Check documentation for more examples.
http://www.thymeleaf.org/doc/articles/standardurlsyntax.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With