【问题标题】:Spring Boot: getting wrong format of double qoutesSpring Boot:双引号格式错误
【发布时间】:2020-12-29 00:59:44
【问题描述】:

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

在我想发送一些 JSON 之前,我没有遇到任何问题。

双qoute(")改为"

因此我遇到了错误。

控制器:

@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"}]

统计/主模板中的 JavaScript 代码:

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

Chrome 开发者标签中的变量“a”来源:

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

谁能告诉我问题出在哪里以及如何解决?

【问题讨论】:

标签: javascript java json spring-boot thymeleaf


【解决方案1】:

试试这个

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

来自Thymeleaf 3.0 docs

请注意,虽然 [[...]] 对应于 th:text(即结果将被 HTML 转义),但 [(...)] 对应于 th:utext 并且不会执行任何 HTML 转义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2019-05-21
    • 2019-10-11
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多