Reputation: 27886
I'm getting this error:
Compilation error [package views.json.Runs does not exist]
when it clearly does exist. I can't figure out what I could be doing wrong.
the action in the Runs
controller:
@BodyParser.Of(play.mvc.BodyParser.Json.class)
public static Result view(Long task_id, Long run_id) {
Run run = Run.find.byId(run_id);
return ok(views.json.Runs.view.render(run));
}
app/views/Runs/view.scala.json
:
@(run: Run)
{
"started": "@run.started",
"completed": "@run.completed"
}
I've gotten a few html templates working, but this is the first JSON template I've tried with 2.0. I'm not sure what else to try since it's already about as basic as possible. Anyone have any ideas?
Update: I've gotten several suggestions for workarounds, but I'm still interested in knowing how to get the templates working, if only to better understand the changes to 2.0.
Upvotes: 6
Views: 4319
Reputation: 3614
Only html, xml and txt appear to be supported by default. For other file extensions and formats, you'd have to register additional "templateTypes" in $PLAY_HOME/framework/src/sbt-plugin/src/main/scala/PlaySettings.scala
(see also: SBT Settings, near the bottom).
It may be helpful to look at the standard template types definitions which are in $PLAY_HOME/framework/src/play/src/main/scala/play/api/templates/Templates.scala
.
You could also cheat and serve your json from a txt file, but do response().setContentType("application/json")
before calling the render
method.
Upvotes: 7
Reputation: 55798
I still don't understand, why does people want to render theirs JSON with views.
Note: this is the same way as @nico_ekito showed before and I agree with him totally , below code just demonstrates how to select part of the object for JSON
public static Result view(Long task_id, Long run_id){
Run run = Run.find.byId(run_id);
ObjectNode result = Json.newObject();
result.put("started", run.started);
result.put("completed", run.completed);
return ok(Json.toJson(result));
}
Upvotes: 0
Reputation: 1077
I recommend following the documentation and let the Json library serialize your data instead of writing the Json text on your own: See Serving Json Response.
Upvotes: 2
Reputation: 21564
For Json, why don't you directly produce a Json string using the Json helper:
public static Result view(Long task_id, Long run_id) {
Run run = Run.find.byId(run_id);
return ok(play.libs.Json.toJson(run));
}
Upvotes: 3