【问题标题】:Tomcat error page using bookmarkable pages in Wicket在 Wicket 中使用可收藏页面的 Tomcat 错误页面
【发布时间】:2014-01-23 13:08:16
【问题描述】:

我有这样的问题。

当收到 404 HTTP 错误时,我需要显示自定义错误页面。我正在使用 Wicket 1.4 和 Tomcat6。我已经在 web.xml 中实现了这些东西,比如

 <filter-name>wicket.filter</filter-name>
    <url-pattern>/*</url-pattern>         
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
 </filter-mapping>

<error-page>
    <error-code>404</error-code>
    <location>/404</location>
</error-page>

并放置在我的 WebApplication 中的此类代码中:

mount(new HybridUrlCodingStrategy("/404", PageNotFound.class));

PageNotFound 类 isErrorPage 设置为 true,isVersioned 设置为 false 和

    @Override
    protected void configureResponse() {
       super.configureResponse();   
       getWebRequestCycle().getWebResponse().getHttpServletResponse().setStatus(HttpServletResponse.SC_NOT_FOUND);
    }

现在的问题是,当我输入一些无效的 url,比如http://localhost:8080/myApp-war/invalidUrl,我可以看到我的 PageNotFound。 但是当我输入http://localhost:8080/myApp-war/?wicket:bookmarkablePage=:com.mypackage.invalidUrl 之类的内容时,我只会得到没有任何数据的空白页面。

我注意到,在第一种情况下,tomcat 只显示“HTTP Status 404 - /myApp-war/invalidUrl” 而在另一种情况下,它显示的消息略有不同:“HTTP Status 404 - Unable to load Bookmarkable Page

如何使两种情况显示相同的页面?也许这个&lt;error-page&gt; 标签需要修改?如果您需要,我会为您提供更多信息。

【问题讨论】:

    标签: java tomcat wicket web.xml


    【解决方案1】:

    您的代码和您的期望是完美的。这只是 Wicket 错误或 Tomcat 对 404 Exception 的错误理解。我猜它只是在 1.4.x 版本之前的 Wicket 中。

    解决方法是重写 AbstractRequestCycleProcessor 中的代码,您可以在 WebApplication 类的工厂方法中重写它:

    @Override
    
    protected IRequestCycleProcessor newRequestCycleProcessor() {
    
    return new WebRequestCycleProcessor() {
    
        @Override
        protected IRequestTarget resolveBookmarkablePage(final RequestCycle requestCycle,
                final RequestParameters requestParameters) {
            IRequestTarget target = super.resolveBookmarkablePage(requestCycle, requestParameters);
            if(target == null) {
                return target;
            }
            if(target instanceof WebErrorCodeResponseTarget) {
                WebErrorCodeResponseTarget errorResponse = (WebErrorCodeResponseTarget) target;
                if(HttpServletResponse.SC_NOT_FOUND == errorResponse.getErrorCode()) {
                    return null;
                }
            }
            return target;
        }
    
    };
    

    }

    我将完整的工作示例放入我的 Wicket14 测试存储库 https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-1.4/browse

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2014-10-19
      相关资源
      最近更新 更多