【问题标题】:Not able to Set default CSS variables from model object in Thymeleaf无法从 Thymeleaf 中的模型对象设置默认 CSS 变量
【发布时间】:2022-01-22 01:14:54
【问题描述】:

我在 Thymeleaf 模板的样式标签内设置 CSS 字体变量。字体值来自模型 目的。我还想应用默认颜色,以防模型属性不存在。 我正在使用 elvis 运算符来选择默认值 Elvis Operator link

但是当我渲染模板时,它正在搜索brandingConfig,然后是themeConfig,然后是fontName,但fontName是 不存在于themeconfig中。所以不是选择默认值,而是抛出错误。下面是错误 我得到的

org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常: “brandingConfig?.themeConfig?.fontName”。

org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在“java.util.LinkedHashMap”类型的对象上找不到属性或字段“fontName” - 可能不公开或无效?

下面是我的风格标签。我是 Thymeleaf 的新手,我应该在这里做些什么不同的事情?

<style th:inline="text">
:root {
    --font-name: [[${brandingConfig?.themeConfig?.fontName}?: 'Proxima Nova']];
    --font-url: [[${brandingConfig?.themeConfig?.fontUrl}?: 'https://fonts.googleapis.com/css?family=Proxima+Nova']];
}

【问题讨论】:

  • themeConfig 是地图吗?出于好奇,我想知道“?”是什么?表示型号名称后面; brandingConfig?.themeConf...
  • @atish.s 是的 themeConfig 是一个地图。

标签: thymeleaf


【解决方案1】:

如果这些表达式对我有用:

<style th:inline="text">
:root {
    --font-name: [[${brandingConfig?.themeConfig?.get('fontName')}?: 'Proxima Nova']];
    --font-url: [[${brandingConfig?.themeConfig?.get('fontUrl')}?: 'https://fonts.googleapis.com/css?family=Proxima+Nova']];
}
</style>

我不确定具体原因,但我认为这与安全导航操作员使用地图键的方式有关...

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 1970-01-01
    • 2014-10-30
    • 2013-11-05
    • 2014-11-27
    • 2013-06-03
    • 2020-03-19
    相关资源
    最近更新 更多