2013-08-24 24 views
3

我想要使用Java檢索一個JavaScript變量。我認爲我接近解決方案。Wicket檢索JavaScript變量在java中使用

我想檢索我面板上元素的寬度。爲了達到這個目的,我在面板中添加了一個行爲來添加回調並檢索參數(寬度和高度)。

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())); 

    } 
} 

在JavaScript中,下面的代碼被調用:

function callbackWicketNewImage(element) { 
    var wcall = Wicket.Ajax.get({ 'u': 'callbackUrl', 'ep' : {'width': element.width(), 'height': element.height()}}); 
} 

當JS-代碼被調用時,「響應」方法被調用,但寬度和高度參數的值也沒有變化。他們仍然是'undef'。

+0

也許element.width()和element.height()的返回民主基金?你真的更新JS中的頁面上的這些值或什麼? – Buurman

+0

不幸的不是。 element.width和element.height方法確實返回正確的值。 – Bram

回答

2

我固定我的問題與下面的代碼。請注意,抽象的onResponse方法是用於抽象的目的。

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); 
} 

行爲電話:

add(new SizeBehavior() { 
    @Override 
    public void onResponse(double width, double height) { 
     //set vars.. 
     widthVar = width; 
     heightVar = height; 
    } 
}); 
1

response方法應該是這樣的:

@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 
    } 

您使用getRequestParameters()。它應該是getQueryParameters()

編輯:還看到這篇文章:http://thombergs.wordpress.com/2013/01/07/rolling-your-own-ajax-behavior-with-wicket

+0

不幸的是,這並沒有改變任何東西。 – Bram