Configure 404 error page in Wicket

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)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
parladAchilliesKumaranAnDyRodrigo Recent comment authors
newest oldest most voted
parlad
Guest
parlad

what actually did, pls anybody can explain.

Achillies
Guest
Achillies

Hi mykong,

My project uses Wicket 1.5.21. I have tried this solution for my project, but it does not work me. I am always being redirected to a “Blank” page for 404 error. Does the work only in later versions of wicket ?

Thanks

Achillies
Guest
Achillies

Sorry I meant Wicket 1.4.21

Kumaran
Guest
Kumaran

Its not working in IE 9 browser but working in Mozilla * opera browsers

AnDy
Guest
AnDy

Hello,

The example is very good and I have to say I use it with small changes (add some logging messages using SLF4J).
My problem is I would like (correct term might be “have to”) to log what was the requested resource that was not found (http://localhost:8080/WicketExamples/wdqwdqdqw).
Do you have any idea / hint how I can do that?

Rodrigo
Guest
Rodrigo

Objective and it works. Thank you for this post.

Earl
Guest
Earl

Just a note, this doesn’t work consistently for Brix (Wicket-based CMS) when ajax/wiquery is being used. Thought there should be a comment about it.

Thibaud
Guest
Thibaud

It works !

Thanks