Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates Playframework 1.2.4:是否将render()的结果作为字符串获取?_Templates_Playframework_Playframework 1.x - Fatal编程技术网

Templates Playframework 1.2.4:是否将render()的结果作为字符串获取?

Templates Playframework 1.2.4:是否将render()的结果作为字符串获取?,templates,playframework,playframework-1.x,Templates,Playframework,Playframework 1.x,在play framework 1.2.4控制器中,是否可以在输出到浏览器之前将模板或标记的内容作为字符串获取 我希望能够做到这一点: String json = renderAsString("/path/to/template.json", var1, var2); //then use json as the body of a Play.WS request body. 该解决方案基于这样一个假设,即您正在谈论PlayFramework 1.x 如果您正在使用: 也可以使用命名参数:

在play framework 1.2.4控制器中,是否可以在输出到浏览器之前将模板或标记的内容作为字符串获取

我希望能够做到这一点:

String json = renderAsString("/path/to/template.json", var1, var2);

//then use json as the body of a Play.WS request body.

该解决方案基于这样一个假设,即您正在谈论PlayFramework 1.x

如果您正在使用:

也可以使用命名参数:

Map<String, Object> args = ...
String s = Rythm.render("path/to/template.suffix", args);
Map args=。。。
字符串s=Rythm.render(“path/to/template.suffix”,args);
注意如果模板文件放在
app/Rythm
文件夹下,Groovy方法也适用于Rythm。

除了

如果您希望创建json,那么最好使用gson,而不是使用groovy模板构建自己的字符串。Gson包含在Play Framework 1.2.X中

你可以找到更多的信息。Gson文档中的示例:

class BagOfPrimitives {
    private int value1 = 1;
    private String value2 = "abc";
    BagOfPrimitives() {
        // no-args constructor
    }
}


BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
//json is {"value1":1,"value2":"abc"}

您也可以使用Flexjson而不是gson。

我已经在使用org.codehaus.jackson.map.ObjectMapper来实现这一点。我只是想要一种方式,将JSON文件格式显示为模板,而不是隐藏在幕后。不确定其中一个是否比另一个好,但可能有性能方面的考虑(但我现在并不真正关心)。如果使用groovy作为模板,那么它可能比gson或jackson慢,如果使用rythm或japid,模板方法的性能优于gson/jackson,因为它们本质上都是静态编译的java类
Map<String, Object> args = ...
String s = Rythm.render("path/to/template.suffix", args);
class BagOfPrimitives {
    private int value1 = 1;
    private String value2 = "abc";
    BagOfPrimitives() {
        // no-args constructor
    }
}


BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
//json is {"value1":1,"value2":"abc"}