Excerpt | ||
---|---|---|
| ||
|
How do I add custom error pages?
...
There are also some other neat settings you should check out in getApplicationSettings()
, getDebugSettings()
, getExceptionSettings()
, getMarkupSettings()
, getPageSettings()
, getRequestCycleSettings()
, getSecuritySettings
and getSessionSettings()
. See the javadoc for more information.
If calling setInternalErrorPage()
, make sure to also call:
Code Block |
---|
// show internal error page rather than default developer page getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettingsExceptionSettings.SHOW_INTERNAL_ERROR_PAGE); |
...
This is assuming that you have mapped "/404" in your WebApplication:
Code Block |
---|
mountmountPage(new HybridUrlCodingStrategy("/404", PageNotFound.class)); |
In your error WebPage:
Code Block |
---|
public class PageNotFound extends WebPage { private static final long serialVersionUID = 1L; public PageNotFound() { // Add any necessary components } @Override protected void configureResponse() { super.configureResponse(); getWebRequestCycle().getWebResponse().getHttpServletResponse().setStatus(HttpServletResponse.SC_NOT_FOUND); } @Override public boolean isVersioned() { return false; } @Override public boolean isErrorPage() { return true; } } |
An HTTP error can be thrown manually by:
Code Block |
---|
// also see AbortWithWebErrorCodeException and AbortWithHttpStatusException
throw new AbortWithHttpStatusException(404, true);
|
For wicket 6 and beyond:
Code Block |
---|
throw new AbortWithHttpErrorCodeException(404, "Some message");
|
Overriding All Error Pages for RuntimeException
Sometimes it is desirable to override all of Wicket's error pages with a custom error page(s) when any RuntimeException occurs. Here is an example of how to do so.
Code Block |
---|
public final class MyRequestCycle extends WebRequestCycle {
/**
* MyRequestCycle constructor
*
* @param application the web application
* @param request the web request
* @param response the web response
*/
public WebRequestCycle(final WebApplication application, final WebRequest request, final Response response) {
super(application, request, response);
}
/**
* {@inheritDoc}
*/
@Override
protected final Page onRuntimeException(final Page cause, final RuntimeException e) {
// obviously you can check the instanceof the exception and return the appropriate page if desired
return new MyExceptionPage(e);
}
}
|
Then in your WebApplication class :
Code Block |
---|
/**
* {@inheritDoc}
*/
@Override
public final RequestCycle newRequestCycle(final Request request, final Response response) {
return new MyRequestCycle (this, (WebRequest)request, (WebResponse)response);
}
|
How do I add/display errors?
...
Code Block |
---|
getSession().error(message); throw new RestartResponseException(MyErrorPage.class, optionalPageParameters); // use RestartResponseAtInterceptPageException(MyErrorPage.class); // instead to interrupt current request processing and immediately redirect to an intercept page // it may be tempting to do the following, but it should not be done: // error(message); // setResponsePage(MyErrorPage.class); // Why not? // What about this; will it work? (My experience says no, but I don't know why not.) // getSession().error(message); // setRedirect(true); // setResponsePage(MyErrorPage.class); |
...