Reputation: 4613
I am trying to retrieve a javascript variable using Java. I think that I am close to the solution.
I want to retrieve the width of an element on my panel. To achieve this, I added a behavior to my panel that adds the callback and retrieves the parameters (width and height).
private class ImageBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void respond(AjaxRequestTarget target) {
//I receive a JavaScript call :)
StringValue width = getRequest().getRequestParameters().getParameterValue("width");
StringValue height = getRequest().getRequestParameters().getParameterValue("height");
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getExtraParameters().put("width", "undef");
attributes.getExtraParameters().put("height", "undef");
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
response.render(OnDomReadyHeaderItem.forScript(getCallbackScript().toString()));
}
}
In javascript, the following code gets called:
function callbackWicketNewImage(element) {
var wcall = Wicket.Ajax.get({ 'u': 'callbackUrl', 'ep' : {'width': element.width(), 'height': element.height()}});
}
When the js-code is called, the 'respond' method gets called but the values of the width and height parameter did not change. They remained 'undef'.
Upvotes: 3
Views: 3155
Reputation: 4613
I fixed my problem with the following code. Please note that the abstract onResponse method is for abstraction purposes.
public abstract class SizeBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void respond(AjaxRequestTarget target) {
IRequestParameters request = RequestCycle.get().getRequest().getRequestParameters();
onResponse(request.getParameterValue("widthParam").toDouble(),
request.getParameterValue("heightParam").toDouble());
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getDynamicExtraParameters()
.add("return {'widthParam': $(" + getComponent().getMarkupId() + ").width(), " +
"'heightParam': $(" + getComponent().getMarkupId() + ").height()}");
}
public void renderHead(Component component, IHeaderResponse response) {
response.render(OnLoadHeaderItem.forScript(getCallbackScript().toString()));
}
public abstract void onResponse(double width, double height);
}
Behavior call:
add(new SizeBehavior() {
@Override
public void onResponse(double width, double height) {
//set vars..
widthVar = width;
heightVar = height;
}
});
Upvotes: 2
Reputation: 20029
Your response
method should look like this:
@Override
protected void respond(AjaxRequestTarget target) {
RequestCycle cycle = RequestCycle.get();
WebRequest webRequest = (WebRequest) cycle.getRequest();
StringValue param1 = webRequest.getQueryParameters().getParameterValue("param1");
StringValue param2 = webRequest.getQueryParameters().getParameterValue("param2");
// do whatever you need with param1 and param2
}
You use getRequestParameters()
. It should be getQueryParameters()
EDIT: see also this article: http://thombergs.wordpress.com/2013/01/07/rolling-your-own-ajax-behavior-with-wicket
Upvotes: 1