使用HeaderResponseContainer:请求周期中不存在FilteringHeaderResponse

时间:2014-08-02 00:42:38

标签: wicket

我尝试在我的wicket应用程序中添加自定义HeaderResponseContainertutorial看起来很简单(参见贡献的定位),但是当我添加这些行并运行应用程序时,我得到IllegalStateException

java.lang.IllegalStateException: No FilteringHeaderResponse is present in the request cycle.  This may mean that you have not decorated the header response with a FilteringHeaderResponse.  Simply calling the FilteringHeaderResponse constructor sets itself on the request cycle
     at org.apache.wicket.markup.head.filter.FilteringHeaderResponse.get(FilteringHeaderResponse.java:165)
     at org.apache.wicket.markup.head.filter.HeaderResponseContainer.onComponentTagBody(HeaderResponseContainer.java:64)
     at org.apache.wicket.markup.html.panel.DefaultMarkupSourcingStrategy.onComponentTagBody(DefaultMarkupSourcingStrategy.java:71)
     ...

是的,我已经看过关于FilteringHeaderResponse的说明。但我不知道应该在哪里调用构造函数。在调用renderHead之前,我已尝试在response.render中添加它,但我仍然得到相同的例外:

public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        FilteringHeaderResponse resp = new FilteringHeaderResponse(response);
        resp.render(new FilteredHeaderItem(..., "myKey"));
}

2 个答案:

答案 0 :(得分:2)

您可以创建一个包装响应的装饰器FilteringHeaderResponse

public final class FilteringHeaderResponseDecorator implements IHeaderResponseDecorator {

    @Override
    public IHeaderResponse decorate(IHeaderResponse response) {
        return new FilteringHeaderResponse(response);
    }
}

在应用程序初始化期间设置它:

Override
public void init() {
    super.init();

    setHeaderResponseDecorator(new FilteringHeaderResponseDecorator());
}

答案 1 :(得分:2)

我刚遇到同样的问题,发现Wicket In Action教程省略了关于在主Wicket Application init中设置自定义IHeaderResponseDecorator的部分。 Wicket指南有一个更全面的例子: Apache Wicket User Guide - Put JavaScript inside page body

你的检票口需要这样的东西:

 @Override
public void init()
{
   setHeaderResponseDecorator(new JavaScriptToBucketResponseDecorator("myKey"));
}

 /**
 * Decorates an original IHeaderResponse and renders all javascript items
 * (JavaScriptHeaderItem), to a specific container in the page.
 */
static class JavaScriptToBucketResponseDecorator implements IHeaderResponseDecorator 
{

    private String bucketName;

    public JavaScriptToBucketResponseDecorator(String bucketName) {
        this.bucketName = bucketName;
    }

    @Override
    public IHeaderResponse decorate(IHeaderResponse response) {
        return new JavaScriptFilteredIntoFooterHeaderResponse(response, bucketName);
    }

}