It’s always recommended to customize a nice error page for “404 page not found” error. This guide show you how to configure a 404 error-page in Wicket.

1. Error Page

Create error page and class for Wicket.

package com.mkyong.user;
 
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
 
public class ErrorPage404 extends WebPage {
 
	public ErrorPage404(final PageParameters parameters) {
 
		add(new Label("404", "Page Not Found!"));
 
	}
}
<html>
<body>
	<h1>Wicket Error 404 example</h1>
 
	<h1><span wicket:id="404"></span></h1> 
 
</body>
</html>

2. Put 404 in web.xml

Specify the “404 error code” in web.xml file, route the common “404 error” to wicket’s “/error404″ file path.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app ...>
 
	<filter>
		<filter-name>wicket.wicketTest</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
			<param-name>applicationClassName</param-name>
			<param-value>com.mkyong.WicketApplication</param-value>
		</init-param>
	</filter>
 
	<filter-mapping>
		<filter-name>wicket.wicketTest</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>ERROR</dispatcher>
	</filter-mapping>
 
	<error-page>
		<error-code>404</error-code>
		<location>/error404</location>
	</error-page>
 
</web-app>
Note
Both dispatcher tags “REQUEST” and “ERROR” are required.

3. Match error to page

In Wicket application, override the init(), match “/error404” to error page “ErrorPage404.html“.

public class WicketApplication extends WebApplication {
 
	@Override
	protected void init() {
 
		mount(new QueryStringUrlCodingStrategy("error404",ErrorPage404.class));
 
	}
 
}

4. Demo

Done, now all not found URL, “404 page not found” error, will redirect to “/error404″, and “/error404″ will display “ErrorPage404.html” file

Figure : If an URL is not found (404), display your custom error page.

wicket 404
Download it – Wicket-404-Example.zip (8KB)
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Apache Wicket Tutorials