RenderHead is not called

I have a CloakDecorator class that 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) { System.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 AjaxLink I create an instance of CloakDecorator :

 AjaxLink link=new AjaxLink("") { @Override public void onClick(AjaxRequestTarget target) { } @Override protected IAjaxCallDecorator getAjaxCallDecorator() { return new CloakDecorator(); } }; 

The problem is that the CloakDecorator constructor is being CloakDecorator , but the renderHead method renderHead not being called. What am I doing wrong? I put some System.out.println in the constructor and in the renderHead method, the System.out.println constructor works, but the second doesn't.

+4
source share
3 answers

Unfortunately, just implementing the IHeaderContributor does not guarantee that you are really contributing to the page / component header. This only works for instances of Component and IBehavior elements that are added to the page and to the page. Specifically from javadoc 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 causes this is in Component#renderHead(HtmlHeaderContainer) . It checks to see if IHeaderContributor implements itself and any of its behavior, and then adds these contributions.

To solve your problem, you can:

  • You have an AjaxLink IHeaderContributor
  • Add IBehavior to your AjaxLink that implements IHeaderContributor

Depending on how often you use this, option # 1 may be the best. Create a "CloakedAjaxLink" that will do everything you need.

+2
source

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

+1
source

I think you need to add the IHeaderContributor component to the component so that it actually β€œcontributes” to the document. The way you use it, you use only half of the required implementation. I suggest you split your implementation or write CloakLink, including this code that can contribute to the page where it is added.

0
source

All Articles