英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论