Play 框架:将 java 方法返回值设置为 javascript var
有没有办法在视图中获取 javascript 中 java 控制器方法返回的值?
我想做的是:
我在控制器方法 X.show() 渲染的视图 showX 中,
我想创建一个对象 y 所以 $.post('@{Y.create()}')
现在我需要 id创建的 y 类型对象的 y 以便在同一视图 (showX) 中使用它。
这可能吗?
Is there's a way to get the value 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你需要的(尽管你的问题很模糊)是从控制器方法返回 JSON。
例如,在您的控制器中,您可以执行以下操作
,然后您将使用
$.post
从 javascript 调用myActionOne
。如果您尚未使用 PlayjsAction
标签,我还建议您查看它。这将返回该对象的 JSON 表示形式。然后,您可以获取所需的任何信息,并以相同的方式调用第二个操作。同样,在第二个操作中,我建议使用
jsAction
,因为它使将参数传递到操作中变得更加容易。编辑:
根据您的编辑,您需要做的就是在控制器方法 Y.create 中执行类似的操作,
显然您创建对象的代码会有所不同,但您明白了。然后,您可以使用标准 javascript 从 JQuery post 响应中获取数据,并访问已返回的 id。
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
And then you will call
myActionOne
from your javascript using$.post
. I would also suggest looking at the PlayjsAction
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
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.
你问的问题太模糊了。但您可能需要 AJAX 来获取此类值。
看一下这里: http://www.oracle.com/technetwork/文章/javaee/ajax-135201.html
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