0

我的视图页面中有 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 controllers.Application.check(dict:Dictionary)

4

1 回答 1

0

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. 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. Let say: /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:

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:

var currentTree = $("#tree").dynatree("getTree").toDict();
$.post("@controllers.routes.Application.check()", { recieved: currentTree},
     function(data) {
        $("#output").html(data);
     });
于 2012-07-22T13:41:08.303 回答