gpt4 book ai didi

javascript - Thymeleaf 未转义的 JavaScript 内联

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:07 30 4
gpt4 key购买 nike

如何使用 Thymeleaf 3.0.x 获得未转义 JavaScript 内联输出?转义内联工作得很好。示例:

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

小服务程序:

model.addAttribute("test", "testing...");

html 模板:

<script th:inline="javascript">
/*<![CDATA[*/
[[${test}]]
[(${test})]
/*]]>*/
</script>

生成的输出:

<script>
/*<![CDATA[*/
'testing...'
[(${test})]
/*]]>*/
</script>

因此,转义表达式 [[ ]] 有效,但非转义表达式 [( )] 无效。我需要有条件地生成 js,并且没有“简单”的解决方法,所以这会非常有帮助。有没有人能让这个工作?非常感谢任何帮助!

最佳答案

我终于让它与具有以下四个依赖项的 Spring Boot 一起工作,所有四个都是必需的(我使用的是当前可用的最新版本):

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.1.2</version>
</dependency>

希望这对您有所帮助。

关于javascript - Thymeleaf 未转义的 JavaScript 内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817612/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com