【问题标题】:Thymeleaf: Could not parse as expressionThymeleaf:无法解析为表达式
【发布时间】: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)

你能帮忙吗?谢谢。

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案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}" />
        

        【讨论】:

          猜你喜欢
          • 2013-04-13
          • 2017-03-28
          • 2018-04-02
          • 1970-01-01
          • 2019-01-04
          • 2017-09-22
          • 2021-09-16
          • 2021-01-02
          • 2018-09-29
          相关资源
          最近更新 更多