簡體   English   中英

如何在Play框架中將字典對象從Ajax傳遞到控制器類

[英]how to pass dictionary object from ajax to controller class in play framework

我的視圖頁面中有dynatree,我想在每次拖放操作中保存樹的當前狀態,問題是我無法將該dict對象傳遞給控制器​​。 ajax的代碼如下:

 onDrop: function(node, sourceNode, hitMode, ui, draggable) {
        /** This function MUST be defined to enable dropping of items on
         * the tree.
         */
        logMsg("tree.onDrop(%o, %o, %s)", node, sourceNode, hitMode);
        sourceNode.move(node, hitMode);
        //save state to backend
     var currentTree = $("#tree").dynatree("getTree").toDict();
            $.post("/Application.java", { recieved: currentTree},
               function(data) {
                 $("#output").html(data);
             });


         //expand the drop target
        sourceNode.expand(true);
      },

我已經在Java中編寫了一個獲取該字典對象的方法。但是它給了我路由文件錯誤。 錯誤:未找到:在此行中鍵入Dictionary

POST / hello /:dict控制器.Application.check(dict:Dictionary)

您的代碼中有很多問題。

首先,對於主要問題,無法編譯routes文件,因為它確實知道您的類Dictionary 因此,您必須告訴文件此類的合格名稱(包+類):

POST /hello/:dict controllers.Application.check(dict:package.to.Dictionary)

但是AFAIK可能也不起作用,因為Url( dict )的動態部分應該可在Url中打印,因為您必須調用編碼有dict的Url。 假設: /hello/thisIsMyDict

因此,最好的選擇是在路由文件中刪除網址的動態部分:

POST /hello/ controllers.Application.check()

然后,在您的操作中,使用DynamicForm例如獲取主體參數:

public static Result check() {
   DynamicForm myForm = form().bindFromRequest();
   String dict = myForm.get("recieved");
   // convert the string to your Dictionary object
   ...
}

最后,您的JavaScript代碼應如下所示:

var currentTree = $("#tree").dynatree("getTree").toDict();
$.post("@controllers.routes.Application.check()", { recieved: currentTree},
     function(data) {
        $("#output").html(data);
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM