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

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

Spring Boot: getting wrong format of double qoutes

问题

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

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

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

双引号(")被改成了"

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

控制器:

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

System.out.println(jsonArray) 输出:

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

statistics/main 模板中的 JavaScript 代码:

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

Chrome 开发者工具中变量 a

  1. 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:

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

System.out.println(jsonArray) output:

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

JavaScript code in statistics/main template:

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

Variable "a" in Chrome developer tab Sources:

  1. 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

尝试一下

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

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

英文:

Try this

  1. 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:

确定