开发者

renderHead is not called

I have a class CloakDecorator which implements IAjaxCallDecorator and IHeaderContributor:

public class CloakDecorator implements IAjaxCallDecorator, IHeaderContributor {

    @SuppressWarnings("unused")
    private static final ResourceReference INDICATOR = new ResourceReference(CloakDecorator.class, "indicator.gif");
    private static final ResourceReference JS = new JavascriptResourceReference(CloakDecorator.class, "CloakDecorator.js");
    private static final ResourceReference CSS = new ResourceReference(CloakDecorator.class, "CloakDecorator.css");

    public CloakDecorator() {
        System.out.println("Constructor");
    }

    public void renderHead(final IHeaderResponse response) {
        Sys开发者_高级运维tem.out.println("renderHead");
        response.renderCSSReference(CSS);
        response.renderJavascriptReference(JS);
    }

    @Override
    public CharSequence decorateScript(CharSequence script) {
        return script;
    }

    @Override
    public CharSequence decorateOnSuccessScript(CharSequence script) {
        return script;
    }

    @Override
    public CharSequence decorateOnFailureScript(CharSequence script) {
        return script;
    }
}

Now from an AjaxLink I am instantiating CloakDecorator:

AjaxLink link=new AjaxLink("") {

    @Override
    public void onClick(AjaxRequestTarget target) {

    }

    @Override
    protected IAjaxCallDecorator getAjaxCallDecorator() {
        return new CloakDecorator();
    }
};

The problem is that the constructor of CloakDecorator is called but the renderHead method is not called. What I am doing wrong? I have placed some System.out.println in constructor and in renderHead method, the System.out.println of constructor is working but the second one not.


Sadly, simply implementing IHeaderContributor does not guarantee that you will actually contribute to the header of the page/component. This only works for instances of Component and IBehavior elements that are added to the page and the page itself. Specifically from the javadoc of IHeaderContributor:

An interface to be implemented by components or behaviors that wish to 
contribute to the header section of the page.

The specific code that calls this is in Component#renderHead(HtmlHeaderContainer). It checks to see if itself and any of its behaviors implement IHeaderContributor and then adds those contributions.

To solve your problem, you can either:

  1. Have your AjaxLink implement IHeaderContributor
  2. Add an IBehavior to your AjaxLink that implements IHeaderContributor

Depending on how often you will use this, Option #1 might be the best. Create a "CloakedAjaxLink" that does all that you need.


Since Wicket 1.5 IAjaxCallDecorators can also contribute to the header if they implement IComponentAwareHeaderContributor.


I think you need to add a IHeaderContributor to a component to make it actually "contribute" to the document. The way you're using it you're using only half of the implementation you want. I suggest you to split your implementation or to write a CloakLink including this code which will be able to contribute to the page where its added.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜