简体   繁体   English

Thymeleaf将JSON字符串作为JSON对象打印到javascript变量中

[英]Thymeleaf print JSON string as JSON object into a javascript variable

In Specific 在具体

I need a way to print JSON representation of a string value into the html page via thymeleaf. 我需要一种方法通过thymeleaf将字符串值的JSON表示打印到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的字符串表示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 :)

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 请参阅https://github.com/thymeleaf/thymeleaf/issues/395中 The Thymeleaf textual syntax

// 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提到的,报告了两个问题。

  1. unescaped inline for scripts/css #12 unescaped inline for scripts / css#12

  2. Use Jackson for Javascript inlining of JSON #81 使用Jackson进行JSON#81的Javascript内联

@Faraj, new version of Thymeleaf provides this functionality. @Faraj,Thymeleaf的新版本提供此功能。 They implement features for the issues that you mentioned. 他们为您提到的问题实现功能。 You can look here: http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html 您可以在这里查看: http//www.thymeleaf.org/doc/articles/thymeleaf3migration.html

The main features: 主要特点:

  • Three textual template modes: TEXT, JAVASCRIPT and CSS. 三种文本模板模式:TEXT,JAVASCRIPT和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). 智能转义JavaScript(作为文字)和CSS(作为标识符)。
  • 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 (/*[# ...]*/) . 通过在注释(/*[# ...]*/)中包装元素和/或输出表达式,将自然模板应用于JAVASCRIPT脚本和CSS样式表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM