I am creating a test application that simply creates a JSON representation of an object and sends it to the template, I then want to use the JSON in a a JS script on the front end. I am using Ratpack and Java Handlebars to do this.
我正在创建一个测试应用程序,它只是创建一个对象的JSON表示并将其发送到模板,然后我想在前端的JS脚本中使用JSON。我正在使用Ratpack和Java Handlebars来做到这一点。
Here is my Ratpack handler
这是我的Ratpack处理程序
class HighChartHandler extends InjectionHandler {
void handle(Context ctx, TestDataJson testDataJson) {
testDataJson.goals = 1000
testDataJson.name = "Nick"
def jsonData = json(testDataJson)
ctx.render(handlebarsTemplate('highchartTest.html', model: jsonData))
}
}
And then I try to simply render the data on the page using
然后我尝试使用简单地在页面上呈现数据
<h1>Graph Test</h1>
<p>This is a WIP highchart test</p>
<p>{{model}}</p>
However I get this message :
但是我收到这条消息:
ratpack.jackson.internal.DefaultJsonRender@467db85c
ratpack.jackson.internal.DefaultJsonRender@467db85c
I want to simply render something like
我想简单地渲染类似的东西
{"name":"Forlan","goals":1000}
2 个解决方案
#1
1
The method you're using, Jackson.json(Object)
https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#json-java.lang.Object- whose return type is of JsonRender
https://ratpack.io/manual/current/api/ratpack/jackson/JsonRender.html
您正在使用的方法,Jackson.json(Object)https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#json-java.lang.Object-其返回类型为JsonRender https ://ratpack.io/manual/current/api/ratpack/jackson/JsonRender.html
So when you're running it through the template, it's simply calling "JsonRender#toString()which results in what you're seeing:
ratpack.jackson.internal.DefaultJsonRender@467db85c`
因此,当您通过模板运行它时,它只是调用“JsonRender#toString()”,这会产生您所看到的内容:ratpack.jackson.internal.DefaultJsonRender @ 467db85c`
The Jackson.json
method returns what is known in Ratpack as a Renderer
. It tells Ratpack how to represent the Object that you've provided to the Renderer
.
Jackson.json方法返回Ratpack中已知的Renderer。它告诉Ratpack如何表示您提供给渲染器的Object。
In order to produce json mixed with html, I would do something like this:
为了生成与html混合的json,我会做这样的事情:
def jsonData = new groovy.json.JsonOutput.toJson(testDataJson)
ctx.render(handlebarsTemplate('highchartTest.html', model: [model: jsonData]))
I haven't tested this but it should work.
我没有测试过这个,但它应该可行。
#2
-1
Try to use
尝试使用
<p>Name: {{name}}</p>
<p>Goals: {{goals}}</p>
instead of
代替
<p>{{model}}</p>
#1
1
The method you're using, Jackson.json(Object)
https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#json-java.lang.Object- whose return type is of JsonRender
https://ratpack.io/manual/current/api/ratpack/jackson/JsonRender.html
您正在使用的方法,Jackson.json(Object)https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#json-java.lang.Object-其返回类型为JsonRender https ://ratpack.io/manual/current/api/ratpack/jackson/JsonRender.html
So when you're running it through the template, it's simply calling "JsonRender#toString()which results in what you're seeing:
ratpack.jackson.internal.DefaultJsonRender@467db85c`
因此,当您通过模板运行它时,它只是调用“JsonRender#toString()”,这会产生您所看到的内容:ratpack.jackson.internal.DefaultJsonRender @ 467db85c`
The Jackson.json
method returns what is known in Ratpack as a Renderer
. It tells Ratpack how to represent the Object that you've provided to the Renderer
.
Jackson.json方法返回Ratpack中已知的Renderer。它告诉Ratpack如何表示您提供给渲染器的Object。
In order to produce json mixed with html, I would do something like this:
为了生成与html混合的json,我会做这样的事情:
def jsonData = new groovy.json.JsonOutput.toJson(testDataJson)
ctx.render(handlebarsTemplate('highchartTest.html', model: [model: jsonData]))
I haven't tested this but it should work.
我没有测试过这个,但它应该可行。
#2
-1
Try to use
尝试使用
<p>Name: {{name}}</p>
<p>Goals: {{goals}}</p>
instead of
代替
<p>{{model}}</p>