use*_*173 3 javascript java wicket
我正在尝试从 Wicket 中的 Javascript 调用一些 Java 代码。
这是我的Java代码:
public ShowUnternehmen() {
add(new AbstractDefaultAjaxBehavior() {
@Override
protected void respond(AjaxRequestTarget ajaxRequestTarget) {
System.out.println("respond");
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead( component, response );
System.out.println(getCallbackUrl());
}
});
Run Code Online (Sandbox Code Playgroud)
}
这是 Javascript 代码:
<wicket:head>
<script type="text/javascript" >
$(function() {
$.contextMenu({
selector: '.context-menu-one',
callback: function(key, options) {
var m = "clicked: " + key;
alert("BLA");
Wicket.Ajax.get({"u":"./com.emg.panels.unternehmen.ShowUnternehmen?1-1.IBehaviorListener.0-"})
},
items: {
"edit": {name: "Editieren", icon: "edit"},
"quit": {name: "Abbrechen", icon: function(){
return 'context-menu-icon context-menu-icon-quit';
}}
}
});
$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});
</script>
</wicket:head>
Run Code Online (Sandbox Code Playgroud)
但是响应方法永远不会执行。我正在查看其他示例,但它们似乎都令人困惑。
我从 renderHead 方法得到了这个 url
You are on a good way. It would be more relalible if you let wicket render the callback function to the page. The following two examples show how you can do that:
A) Render a global callback function. The disadvantage will be that you will have only one callback endpoint.
(1) Create a behavior like the following one:
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()));
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
response.render(JavaScriptHeaderItem.forScript(String.format("nameOfFunction=%s", getCallbackFunction(CallbackParameter.explicit("yourName"))), "CallFromJavascriptBehavior"));
}}
Run Code Online (Sandbox Code Playgroud)
(2) Add this behavior to your wicket page.
(3) Now you can call nameOfFunction('Markus'); from your javascript.
B) Call a initialisation function for each instance of your component on page.
(1) add a initialisation function to your page
<script>
function initMyComponent(selector, callback){
$(selector).click(function(){
callback("Markus");
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
(2) Create a behavior like the following which calls the initialisation function and passes the necessary selector and callback function.
public class ComponentBehavior 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()));
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
response.render(OnDomReadyHeaderItem.forScript(String.format("initMyComponent('#%s', %s)", component.getMarkupId(), getCallbackFunction(CallbackParameter.explicit("yourName")))));
}}
Run Code Online (Sandbox Code Playgroud)
(3) Add the behavior to your wicket component.
If the response method did not get called this could have different reasons. You should check your console (ide and browser) first.