Question

I have such an issue.

I need to show customized error page, when receive 404 HTTP error. I`m using Wicket 1.4 and Tomcat6. I have implemented these things in web.xml, like

 <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>

and placed in such code in my WebApplication:

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

PageNotFound class isErrorPage set true, isVersioned false and

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

Now the problem is, that when I enter some invalid url, like http://localhost:8080/myApp-war/invalidUrl, I can see my PageNotFound. But when I enter something like http://localhost:8080/myApp-war/?wicket:bookmarkablePage=:com.mypackage.invalidUrl, then I just get empty clear page without any data.

I have noticed, that at first situation tomcat shows just "HTTP Status 404 - /myApp-war/invalidUrl" and in the other case it shows slightly different message : "HTTP Status 404 - Unable to load Bookmarkable Page"

How is it possible to make both cases show the same page? Maybe this <error-page> tag needs to be modified? I`ll provide you with more information, if you need.

Was it helpful?

Solution

Your code and your expectations are perfect. It is just a Wicket bug or Tomcat wrong understanding of 404 Exception. I guess it is just in Wicket prior to 1.4.x version.

The fix is to override the code in AbstractRequestCycleProcessor, you can override it just in the factory method of your WebApplication class:

@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;
    }

};

}

I place the full working example into my Wicket14 testing repository https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-1.4/browse

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top