3

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'.

2
  • Maybe element.width() and element.height() return undef? Do you actually update these values on the page in JS or something? Commented Aug 27, 2013 at 12:07
  • Unfortunately not. The element.width and element.height methods do return the right values. Commented Aug 27, 2013 at 14:19

2 Answers 2

2

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;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Unfortunately this did not change anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.