Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: Could not parse as expression

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.

like image 820
METTAIBI Avatar asked Oct 21 '15 23:10

METTAIBI


2 Answers

You can't embed variables like this in Thymeleaf.

Try this instead:

<img th:src="${'photoCat?idCat=' + cat.idCategorie}" />
like image 76
lyderic Avatar answered Jan 04 '23 20:01

lyderic


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

like image 34
Piyush Avatar answered Jan 04 '23 21:01

Piyush