简体   繁体   English

在游戏中消耗 JSON! 框架 controller

[英]Consuming JSON in play! framework controller

I'm trying to consume a JSON array I created using JavaScript but the array is never bound in my controller我正在尝试使用我使用 JavaScript 创建的 JSON 数组,但该数组从未绑定在我的 controller

Here is the JavaScript code I use to call my controller action这是我用来调用 controller 操作的 JavaScript 代码

$.post("/produits_ajax",{filterParams:[{name:"milk", value:"chevre"}, {name:"pate", value:"molle"}]},
function(data){
    $('.dynamicContent').html(data);
slideProducts();
// initialize scrollable
$(".scrollable").scrollable();

});

My routes file entry我的路线文件条目

POST        /produits_ajax       Application.produitsAjax

Here is how I receive it in my play.这是我在游戏中收到它的方式。 controller. controller。 I'm using play 1.1 and the JsonArray is from com.google.gson.JsonArray我正在使用 play 1.1,而 JsonArray 来自 com.google.gson.JsonArray

public static void produitsAjax(JsonArray filterParams) {
    if(filterParams != null)
        Logger.debug("Le Json: " + filterParams.toString());
    else
        Logger.debug("filterParams is null");

    render();
}

As you can imagine I always get "filterParams is null" in my console (I wouldn't be writhing this if I wasn't)正如你可以想象的那样,我总是在我的控制台中得到“filterParams is null”(如果我不是的话,我不会扭动这个)

It's very basic so far I just want to bind the array generated in JS to my JsonArray.到目前为止,这是非常基本的,我只想将 JS 中生成的数组绑定到我的 JsonArray。 Play.framework has a great documentation but for some reason there is very little on this particular subject. Play.framework 有一个很好的文档,但出于某种原因,关于这个特定主题的内容很少。

If anyone can shed some light on this It would be much appreciated如果有人可以对此有所了解,将不胜感激

You just have to create a TypeBinder class to add JsonObject binding capacity to Play., Actually.您只需创建一个 TypeBinder class 即可将 JsonObject 绑定能力添加到 Play。,实际上。 that's pretty easy to achieve in Play 1.2: Here's the complete class:这在 Play 1.2 中很容易实现:这是完整的类:

@Global
public class JsonObjectBinder implements TypeBinder<JsonObject> {

    @Override
    public Object bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception {
        return new JsonParser().parse(value);
    }

}

That's it, With the @Global annotation.就是这样,使用 @Global 注释。 Play will find it at load time and register this with the other binders: I use this in my controller with the following signature : Play 会在加载时找到它并将其注册到其他活页夹中:我在我的 controller 中使用它,签名如下:

public static void handleJsonBody(JsonObject body) {
    ...
}

The body will be automatically parsed by Play.正文将由 Play 自动解析。 You can do the same for JsonArray to support your particular case.您可以对 JsonArray 执行相同的操作以支持您的特定情况。

You have 2 options:您有 2 个选项:

  • simple one: pass the parameter as a String and in the controller parse it to Json简单的一个:将参数作为字符串传递,并在 controller 中将其解析为 Json
  • complex one: create your own binder using TypeBinder复杂一:使用TypeBinder创建自己的活页夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM