开发者

Play framework: setting a java method return value to a javascript var

Is there's a way to get the v开发者_C百科alue returned by a java controller method in javascript in the views ?

what I want to do is:

i'm in view showX rendered by controller method X.show()

i want to create an object y so $.post('@{Y.create()}')

now i need the id of the created object of type y to use it in the same view (showX).

is that possible?


It sound like what you need (although your question is very vague), is to return JSON from your controller method.

Such as, in your controller, you can do

public static void myActionOne() {
    renderJSON(myObject);
}

And then you will call myActionOne from your javascript using $.post. I would also suggest looking at the Play jsAction tag if you are not already using it. This will return a JSON representation of the object. You can then take whatever information you need and call a second action in the same way.

Again, in the second action, I would suggest jsAction, as it makes passing parameters into your actions far easier.

EDIT: Based on your edit, then all you need to do is in your controller method Y.create, do something like

public static void create() {
    MyObject obj = new MyObject();
    obj.save();
    Long id = obj.id;
    renderJSON(id);
}

Obviously your code to create your object will be different, but you get the idea. You can then just take the data from the JQuery post response, and access the id that has been returned, using standard javascript.


You question is too vague. But you probably will need of AJAX to get a value of this kind.

Take a look here: http://www.oracle.com/technetwork/articles/javaee/ajax-135201.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜