【问题标题】:Thymeleaf image src + ParseException: Exception evaluating SpringEL expression: "currentUser.get('profilePicture').get('url')"Thymeleaf 图像 src + ParseException:评估 SpringEL 表达式的异常:“currentUser.get('profilePicture').get('url')”
【发布时间】:2017-04-06 04:55:52
【问题描述】:

我从ApplicationAdvice 类中获得一个 currentUser,如果用户已登录,则将其显示在每个页面上:

@ModelAttribute("currentUser")
public TmtUser currentUser(Model model) {

    TmtUser currentUser = TmtUser.currentUser;
    model.addAttribute("currentUser", currentUser);

    return currentUser;
}

currentUser 对象有一个可用的url 属性。但我在将 url 呈现到页面时遇到错误。也许在提供 url 值之前渲染了 DOM?我怎么知道?

HTML:

<img th:src="${currentUser.get('profilePicture').get('url')}" src="" />

例外:

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "currentUser.get('profilePicture').get('url')" (template: "fragments/header" - line 72, col 139)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.2.RELEASE.jar:2.0.2.RELEASE]
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
    ... 105 common frames omitted

对象模型:

浏览器控制台错误:

org.parse4j.ParseFile@190687b3 Failed to load resource: the server responded with a status of 404 ()

似乎 ParseFile 本身没有加载到浏览器中。

【问题讨论】:

    标签: java html image thymeleaf parse4j


    【解决方案1】:

    你认为这个表达是什么意思?

    currentUser.get('profilePicture').get('url')
    

    currentUser 对象是否有类似这样的方法?

    public class TmtUser {
        public Map<String, Object> get(String key) {
            ...
            ...
        }
    }
    

    因为这就是你想要做的事情。但是根据你的对象的图片,没有这样的东西。当您制作这些表达式时,您需要考虑它在 java 中的外观。例如,在 java 中它可能看起来像这样:

    currentUser.getData().get("profilePicture").getUrl()
    

    翻译成一个表达式,它看起来像:

    ${currentUser.data['profilePicture'].url}
    or
    ${currentUser.data.get('profilePicture').url}
    

    (由于我不熟悉 ParseFile,所以我不确定它是否具有 url 属性的获取和设置器)

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2019-05-24
      • 2018-02-20
      相关资源
      最近更新 更多