简体   繁体   English

如何在Play框架中将字典对象从Ajax传递到控制器类

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

I have dynatree in my view page, I want to save the current state of the tree on every drag and drop operation, The problem is I am unable to pass that dict object to the controller. 我的视图页面中有dynatree,我想在每次拖放操作中保存树的当前状态,问题是我无法将该dict对象传递给控制器​​。 The code of ajax is below : 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);
      },

I have written a method in java that is getting that dictionary object.. But It is giving me error in route file. 我已经在Java中编写了一个获取该字典对象的方法。但是它给了我路由文件错误。 Error : not found: type Dictionary in this line 错误:未找到:在此行中键入Dictionary

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

You have many problems in your code. 您的代码中有很多问题。

First, for the main issue, the routes files can not be compiled because it does know your class Dictionary . 首先,对于主要问题,无法编译routes文件,因为它确实知道您的类Dictionary So you have to tell the file the qualified name of this class (package + class): 因此,您必须告诉文件此类的合格名称(包+类):

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

But AFAIK, it may not work either, because the dynamic part of the Url ( dict ) should be printable in the Url, because, you'll have to call the Url with your dict encoded into it. 但是AFAIK可能也不起作用,因为Url( dict )的动态部分应该可在Url中打印,因为您必须调用编码有dict的Url。 Let say: /hello/thisIsMyDict 假设: /hello/thisIsMyDict

So your best option is to remove the dynamic part of the Url in the routes file: 因此,最好的选择是在路由文件中删除网址的动态部分:

POST /hello/ controllers.Application.check()

Then, in your action, get the body parameters using a DynamicForm for instance: 然后,在您的操作中,使用DynamicForm例如获取主体参数:

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

And finally, your javascript code should look like: 最后,您的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.

相关问题 如何在Spring MVC中将JSON对象从Ajax传递到控制器? - How to pass JSON object from ajax to controller in spring mvc? 如何使用Spring Security将对象从AJAX传递到Spring Controller - How to pass an Object from AJAX to a Spring Controller with Spring Security 如何将Java对象传递到Play的模板中? [播放框架2.0] - How to pass Java object into Play's template? [Play framework 2.0] 从控制器显示日期对象/值以在Play Framework / Scala中查看 - Display date object/value from controller to view in Play Framework/Scala 如何在play(2.3)框架内的Java控制器代码中从String变量构造Html对象 - How do I construct an Html object from a String variable in Java controller code within the play (2.3) framework 当从视图中引用控制器中的Java类时,Play Framework编译错误“类型索引不是对象控制器的成员。应用程序” - Play Framework compile error “type Index is not a member of object controllers.Application” when referring to Java class in controller from view 如何将数据从POJO和Controller传递到Ajax - How to pass data from POJO and Controller to Ajax Play Framework 1.2.7将表单值传递给控制器 - Play Framework 1.2.7 pass form values to controller 如何从ajax将JSONArray传递给控制器 - how to pass JSONArray to the controller from ajax 播放框架-具有Class参数的路由中的呼叫控制器 - Play framework - call controller in routes with Class parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM