【问题标题】:Adding a model attribute to the html template without encoding在不编码的情况下向 html 模板添加模型属性
【发布时间】:2014-09-08 00:17:32
【问题描述】:

我正在使用 spring boot(版本 1.1.1.RELEASE)并尝试在 html 模板中添加字符串模型属性。

控制器:

@RequestMapping({"/", ""})
public String template(Model model) {
    model.addAttribute("coolStuff", coolStuff);
    return "panel/index";
}

HTML 模板:

<script type="text/javascript" th:inline="text">
/*<![CDATA[*/
    [[${coolStuff}]]
/*]]>*/
</script>

thymeleaf 的 th:inline 在“文本”模式下工作得很好,但现在它正在向提供的字符串添加 HTML 编码(转义字符)。 th:inline in mode "javascript" 转义双引号,因此这也不起作用。 有没有什么方法可以在不编码的情况下将模型属性中的字符串放入 html 模板中?

【问题讨论】:

    标签: spring-boot thymeleaf html-encode modelattribute


    【解决方案1】:

    您可以使用 th:utext 来禁用 Thymeleaf 的转义,但我不知道有什么方法可以将它与 th:inline 结合使用。我认为您仍然可以实现您想要的,但是您必须更改 coolStuff 的值以包含整个 &lt;script&gt; 块。

    【讨论】:

    • 最后我确实像这样使用了 th:utext,coolStuff 是一个 javascript 脚本: ...但这是使用用于获取值的“messageSource”bean。我试图通过模型属性直接注入值。 th:inline 确实读取模型属性,但它会转义字符,例如:“ 。我想如果有人知道 th:uinline (unchecked-inline) 之类的东西,它会完全像 inline 但没有转义任何东西。
    猜你喜欢
    • 2020-08-31
    • 2016-05-10
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多