Spring Boot:获取错误格式的双引号

huangapple go评论67阅读模式
英文:

Spring Boot: getting wrong format of double qoutes

问题

以下是您提供的翻译内容:

我正在编写一个应用程序,在其中我需要从Spring Boot控制器将一些数据通过Thymeleaf发送到模板。

在我想发送一些JSON时,我遇到了问题。

双引号(")被改成了"

因此,我遇到了一个错误。

控制器:

@GetMapping("/statistics")
public String viewStatistics(Model model) {
    JSONArray jsonArray = statisticsService.getTaskNamePercentageMap();
    System.out.println(jsonArray);
    model.addAttribute("taskNamePercentageMap", jsonArray);
    return "statistics/main";
}

System.out.println(jsonArray) 输出:

[{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]

statistics/main 模板中的 JavaScript 代码:

$(document).ready(function () {
    var json =[[${taskNamePercentageMap}]];
    /*... TO BE CONTINUED ...*/
});

Chrome 开发者工具中变量 a

var b = JSON.stringify([{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]);

有人可以告诉我问题出在哪里,以及如何修复吗?

英文:

I am writing an app in which I need to send some data from Spring Boot controller to template by Thymeleaf.

I did not have problems until I wanted to send some JSON.

Double qoute(") is changed to ".

Because of that I am getting an error.

Controller:

@GetMapping("/statistics")
public String viewStatistics(Model model) {
    JSONArray jsonArray = statisticsService.getTaskNamePercentageMap();
    System.out.println(jsonArray);
    model.addAttribute("taskNamePercentageMap", jsonArray);
    return "statistics/main";
}

System.out.println(jsonArray) output:

[{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]

JavaScript code in statistics/main template:

$(document).ready(function () {
        var json =[[${taskNamePercentageMap}]];
        /*... TO BE CONTINUED ...*/
    });

Variable "a" in Chrome developer tab Sources:

var b = JSON.stringify([{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]);

Can someone tell me where is the problem and how to fix it?

答案1

得分: 3

尝试一下

var json = [(${taskNamePercentageMap})];

来自 Thymeleaf 3.0 文档
>请注意,虽然 [[...]] 对应于 th:text(即结果将进行 HTML 转义),[(...)] 对应于 th:utext,不会执行任何 HTML 转义。

英文:

Try this

var json = [(${taskNamePercentageMap})];

From Thymeleaf 3.0 docs
>Note that, while [[...]] corresponds to th:text (i.e. result will be HTML-escaped), [(...)] corresponds to th:utext and will not perform any HTML-escaping.

huangapple
  • 本文由 发表于 2020年9月10日 21:52:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63831211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定