我正在使用胡须创建模板,使用服务器端的 Spring-boot-starter-mustache 来创建模板。
我提到:https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-mustache/
绒球.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
我有一个正在控制器中创建的 Java JSON 对象:
model.put("schoolJson", schoolJson);
return "viewJson";
JSON结构:
{
"school": "abc",
"loc": "def",
"type": [
{
"section": "sec1",
"test": {
"grade": "gradea"
}
},{
"section": "sec2",
"test": {
"grade": "gradeb"
}
}
]
}
在我看来,我想在"类型"中显示每个"测试"的"等级"。但它不会被显示出来。有人可以在这里帮忙吗?
<div>
<p>{{schoolJson.school}}</p>
{{#schoolJson.type}}
<ul>
<li>schoolJson.type.grade</li>
</ul>
{{/schoolJson.type}}
</div>
您的li
应该这样完成:
<li>
{{#test}}
{{grade}}
{{/test}}
</li>
这是因为您必须进入嵌套test
对象的上下文才能获得grade
值
如果"test"键始终存在,您可以编写:
<li>{{test.grade}}</li>
我刚刚检查过,这按预期工作。如果它在代码中的服务器端不起作用,问题是:如何解析 JSON 字符串?JMustache 本身不是 JSON 感知的,您必须将其解析为 jmustache 理解的数据结构。请参阅此示例,了解如何同时使用 noggit 和 JMustache https://stackoverflow.com/a/47485363/868941