Thymeleaf 将 JSON 字符串作为 JSON 对象打印到 javascript 变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29733594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf print JSON string as JSON object into a javascript variable
提问by Faraj Farook
In Specific
在特定
I need a way to print JSON
representation of a string value into the html page via thymeleaf.
我需要一种JSON
通过 thymeleaf 将字符串值的表示形式打印到 html 页面的方法。
In detail
详细
I'm having a model attribute
which contains a string which is actually a string representation of the JSON
我有一个model attribute
包含字符串的字符串,它实际上是JSON
My thymeleaf
code
我的thymeleaf
代码
<script th:inline="javascript">
var value = [[${data.scriptValue}]];
</script>
print the variable as below
打印变量如下
var value = '[[\"asd\",\"3\"],[\"asd\",\"1\"],[\"asdasd\",\"1\"]]';
But I want something like this as a javascript/JSON
array
但我想要这样的东西作为javascript/JSON
数组
var value = [["asd","3"],["asd","1"],["asdasd","1"]];
How to do this in thymeleaf?
如何在百里香中做到这一点?
Note: I know I can do this from
JSON.Parse
but i need a way to do this from thymeleaf :)注意:我知道我可以JSON.Parse
从 thymeleaf 中做到这一点,但我需要一种方法来做到这一点 :)采纳答案by Faraj Farook
Update - 2015/12/24
更新 - 2015/12/24
This feature is available in Thymeleaf 3
此功能在 Thymeleaf 3 中可用
Refer The Thymeleaf textual syntax
in https://github.com/thymeleaf/thymeleaf/issues/395
请参阅The Thymeleaf textual syntax
在https://github.com/thymeleaf/thymeleaf/issues/395
// Unescaped (actual answer)
var value = [(${data.scriptValue})];
//or
var value = [# th:utext="${data.scriptValue}"/];
// Escaped
var value = [[${data.scriptValue}]];
//or
var value = [# th:text="${data.scriptValue}"/];
It's not possible at Thymeleaf 2.As Patric LC mentioned, there are two issues reported for this.
这在 Thymeleaf 2 中是不可能的。正如 Patric LC 所提到的,为此报告了两个问题。
回答by m.aibin
@Faraj, new version of Thymeleaf provides this functionality. They implement features for the issues that you mentioned. You can look here: http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
@Faraj,新版本的 Thymeleaf 提供了此功能。他们为您提到的问题实现了功能。你可以看这里:http: //www.thymeleaf.org/doc/articles/thymeleaf3migration.html
The main features:
主要特点:
- Three textual template modes: TEXT, JAVASCRIPT and CSS.
- New syntax for elements in textual template modes:
[# ...] ... [/]
. - Inlined output expressions allowed, both escaped
([[...]])
and unescaped([(...)])
. - Intelligent escaping of JavaScript (as literals) and CSS (as identifiers).
- Parser-level
(/*[- ... -]*/)
and prototype-only(/*[+ ... +]*/)
comment blocks. - Natural templates applied to JAVASCRIPT scripts and CSS style sheets by means of wrapping elements and/or output expressions inside comments
(/*[# ...]*/)
.
- 三种文本模板模式:TEXT、JAVASCRIPT 和 CSS。
- 文本模板模式中元素的新语法:
[# ...] ... [/]
. - 允许内联输出表达式,包括转义
([[...]])
和非转义([(...)])
。 - JavaScript(作为文字)和 CSS(作为标识符)的智能转义。
- 解析器级
(/*[- ... -]*/)
和仅限原型的(/*[+ ... +]*/)
注释块。 - 通过在注释中包装元素和/或输出表达式,应用于 JAVASCRIPT 脚本和 CSS 样式表的自然模板
(/*[# ...]*/)
。