[英]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.