从Javascript调用Wicket 6代码并返回值 [英] Call Wicket 6 Code from Javascript and return value
问题描述
在此示例中,我设法使用选项A从Javascript调用了Wicket 6 Java代码: https://stackoverflow.com/a/42612027/1047418
I have managed to call my Wicket 6 Java code from Javascript using option A in this example: https://stackoverflow.com/a/42612027/1047418
但是,我找不到用于将数据从Java端返回给JavaScript的示例(生成的JavaScript回调函数甚至都不包含return语句).如何实现?
However, I have not been able to find examples for returning data from the Java side back to JavaScript (the generated JavaScript callback function does not even include a return statement). How can this be achieved?
我没有试图在Java中设置属性,正如我已经解释的那样,从JavaScript调用Wicket并不是这里的问题.由于Ajax请求,我试图将Wicket的JSON对象返回给浏览器.
I am not trying to set an attribute in Java and as I've already explained, calling Wicket from JavaScript is not the problem here. I am trying to return a JSON object from Wicket back to the browser as a result of an Ajax request.
Edit2:按照martin-g的示例,我整理了这个工作示例...
Following martin-g's examples I cobbled up this working example...
Java
public class MyAjaxBehaviour extends AbstractDefaultAjaxBehavior {
@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
tag.put("aprachatcallbackurl", getCallbackUrl());
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setDataType("json");
attributes.setWicketAjaxResponse(false);
}
@Override
protected void respond(AjaxRequestTarget target) {
getComponent().getRequestCycle().replaceAllRequestHandlers(
new TextRequestHandler("application/json", "UTF-8", "{...JSON GOES HERE...}));
}
}
JavaScript
JavaScript
var mySuccessCallback = function(param1, param2, data, statusText) {
// Data contains the parsed JSON object from MyAjaxBehaviour.respond(...)
...
}
var myFailureCallback = function() {
...
}
Wicket.Ajax.get({
"u": callbackUrl,
"dt": "json",
"wr": false,
"sh": [mySuccessCallback],
"fh": [myFailureCallback]
});
主要问题在于,"Wicket 7参考"错误地指示在JavaScript调用中使用"wr"而不是"dt". :)
Main problem as that the Wicket 7 Reference incorrectly instructs to use "wr" instead of "dt" in the JavaScript call. :)
推荐答案
我认为您可以以更简单的方式做到这一点!
I think you can do it in a simpler way!
Wicket Ajax API只是:Wicket.Ajax.ajax({...})
.您需要在服务器端进行的准备工作就是保存回调网址,例如通过将其全局保存在window
对象或HTML元素的属性(data-the-url
)中.
Wicket Ajax API is just: Wicket.Ajax.ajax({...})
. All you need to prepare at the server side is to save the callback url, e.g. by saving it globally in the window
object or in HTML element's attributes (data-the-url
).
public class CallFromJavascriptBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void respond(AjaxRequestTarget target) {
final StringValue parameterValue = RequestCycle.get().getRequest().getQueryParameters().getParameterValue("yourName");
System.out.println(String.format("Hello %s", parameterValue.toString()));
// write anything to the WebResponse and then consume it in the JS success handler. See below
}
@Override
public void onComponenntTag(ComponenntTag tag, Component component) {
super.onComponenntTag(tag, component);
tag.put("data-the-url", getCallbackUrl());
}
}
然后在您的JS代码中可以执行以下操作:
Then in your JS code you can do:
var callbackUrl = jQuery("#theElementId").data("the-url");
Wicket.Ajax.get({"u": callbackUrl, "sh":[successHandler], "fh": [failureHandler] });
其中successHandler
和failureHandler
是内联(例如function(...) {}
)或其他地方定义的JS函数.
Where successHandler
and failureHandler
are JS functions defined inline (e.g. function(...) {}
) or elsewhere.
更多文档,您可以在以下位置找到: https://ci.apache.org/projects/wicket/guide/7.x/single.html#_ajax_request_attributes_and_call_listeners
More documentation you can find at: https://ci.apache.org/projects/wicket/guide/7.x/single.html#_ajax_request_attributes_and_call_listeners
博客文章,其中包含完整示例,网址为 http://wicketinaction. com/2012/07/wicket-6-javascript-improvements/
A blog article with an complete example at http://wicketinaction.com/2012/07/wicket-6-javascript-improvements/
这篇关于从Javascript调用Wicket 6代码并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!