如何使用 Thymeleaf 内联 JavaScript [英] How to inline JavaScript with Thymeleaf

查看:44
本文介绍了如何使用 Thymeleaf 内联 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Thymeleaf 做内联 Javascript.

I would like to use Thymeleaf to do inline Javascript.

例如:

<script th:inline="javascript">
/*<![CDATA[*/
  /*[[${myCode}]]*/;
/*]]>*/
</script>

在 Spring Boot 中我有这个:

and in Spring Boot I have this:

model.addAttribute("myCode", "alert("test")");

我的输出 HTML 是:

My output HTML is:

<script th:inline="javascript">
/*<![CDATA[*/
  "alert("test")";
/*]]>*/
</script>

这是一个字符串.我做错了什么?

which is a string. What am I doing wrong?

推荐答案

When 内联[[...]]对应th:text[(...)]对应到 th:utext.

When inlining, [[...]] corresponds to th:text and [(...)] corresponds to th:utext.

所以

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

这篇关于如何使用 Thymeleaf 内联 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆