Java 不调用renderHead
我有一个类Java 不调用renderHead,java,wicket,Java,Wicket,我有一个类斗篷装饰器,它实现了IAjaxCallDecorator和IHeaderContributor: public class CloakDecorator implements IAjaxCallDecorator, IHeaderContributor { @SuppressWarnings("unused") private static final ResourceReference INDICATOR = new ResourceReference(CloakD
斗篷装饰器
,它实现了IAjaxCallDecorator
和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;
}
}
AjaxLink link=new AjaxLink("") {
@Override
public void onClick(AjaxRequestTarget target) {
}
@Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new CloakDecorator();
}
};
An interface to be implemented by components or behaviors that wish to
contribute to the header section of the page.
现在,我从AjaxLink实例化斗篷装饰器
:
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;
}
}
AjaxLink link=new AjaxLink("") {
@Override
public void onClick(AjaxRequestTarget target) {
}
@Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new CloakDecorator();
}
};
An interface to be implemented by components or behaviors that wish to
contribute to the header section of the page.
问题是调用了
斗篷装饰器
的构造函数,但没有调用renderHead
方法。我做错了什么?我已经将一些System.out.println
放在构造函数和renderHead
方法中,构造函数的System.out.println
正在工作,但第二个没有工作。遗憾的是,简单地实现IHeaderContributor
并不能保证您将实际贡献给页面/组件的标题。这仅适用于添加到页面和页面本身的组件
和IBehavior
元素的实例。特别是来自IHeaderContributor
的javadoc:
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;
}
}
AjaxLink link=new AjaxLink("") {
@Override
public void onClick(AjaxRequestTarget target) {
}
@Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new CloakDecorator();
}
};
An interface to be implemented by components or behaviors that wish to
contribute to the header section of the page.
调用此函数的特定代码位于组件#renderHead(HtmlHeaderContainer)
中。它检查自身及其任何行为是否实现了IHeaderContributor
,然后添加这些贡献
要解决您的问题,您可以:
AjaxLink
实施IHeaderContributor
IBehavior
添加到实现IHeaderContributor的AjaxLink
根据您使用此选项的频率,选项1可能是最好的。创建一个“隐形链接”,完成所有需要的功能。我认为您需要向组件添加IHeaderContributor,使其真正“贡献”文档。按照您使用它的方式,您只使用了所需实现的一半。我建议您拆分您的实现或编写一个包含此代码的DopperLink,它将能够添加到添加页面。因为Wicket 1.5 IAjaxCallDecorators如果实现IComponentAwareHeaderContributor,也可以添加到标题。我使用的是Wicket 1.3.6,你能认真考虑升级到至少WIKET 1.4吗?有一些迁移开销,但它是一个巨大的改进。无论是哪种情况,我认为您都不会像Wicket 1.5中那样获得此功能:)