Wednesday, August 08, 2007

Shortening your Wicket application's URLs on JBoss

Without doing anything special, my login URL was http://host:port/mycontext/app/login/MyLogin. This is too long and unwieldy.

By making some changes in configuration and in code, the new URL is http://host:port/ (which redirects to http://host:port/app/login).

To shorten the URL, I did the following:

In my Wicket project's WEB-INF directory, I added jboss-web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<context-root />

This changes the application context to /, i.e. it removes /mycontext from the URL.

Next, in the Wicket application class init method, I added these lines:

mountBookmarkablePage("/login", MyLogin.class);
...mount all other bookmarkable pages as above...

This has the effect of mapping the login page to http://host:port/app/login. A bookmarkable page is one that does not depend on state from a previous page (although it can depend on state from the session). That is, once the user has logged in, all pages you can reach from navigation links are bookmarkable. On the other hand, pages you get to by submitting a form are typically not bookmarkable.

Finally, I added a redirect by creating the page index.html in the context root (i.e. a sibling to the WEB-INF directory):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta HTTP-EQUIV="REFRESH" content="0; url=/app/login" />


You can but probably should not remove the /app part of the URL. This is a Wicket best practice.

No comments: