thymeleaf的坑(持续更新。。。)

环境声明:
springBoot : 1.5.9
thymeleaf:springBoot默认集成2.16版本(这就是个坑了。。)javascript

一、在thymeleaf模板中动态添加背景图片
语法:
th:stylejava

例子:web

<body th:style="'background:url(' + @{/image/36.jpg} + ');'">
</body>

坑说明:
在thymeleaf 2.16版本,该表达式没法解析。需将thymeleaf版本改为3.0spring

作法:session

<properties>
        <thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>

二、在thymeleaf中获取web上下文路径url

语法:spa

<script th:inline="javascript">
    var username = [[${#httpServletRequest.getContextPath()}]];
</script>

须要在 script 中使用 th:inline="javascript"code

至于 [[]]。 是thymeleaf 直接在取值的一个语法。
例如图片

<p>Hello, [[${session.user.name}]]!</p>

直接从session中取值,该作法等同于ip

<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>
相关文章
相关标签/搜索