- Wicket's Date & Time Components and Client-Server Timezones
- Some useful Java System Properties while starting JVM.
- Jbossws 2.0.1 with JDK 1.6
- Date & Time selection made simple in Wicket
- New Features in Vim 7
- Maven best practices: Use dependency management for multi module projects.
- Prototyping LAMP with WAMP
- Bash shell tricks
The problemIn a typical web application, it is often necessary to work with multiple Timezones on the Client side. On the server side you may wish to store the data in one single "Timezone" preferably "GMT", but it hardly makes sense to display and accept dates in GMT, as this places the onus on the user to translate time in his local timezone, to GMT before entering the time information, and vice-versa for interpreting any dates shown in the Web application. What is required is for the web application to show and display date and times in the User's timezone, and do all the necessary conversions while transmitting the date/time information between the User and the Back end. wicket framework makes this extremely simple to implement. The SolutionWicket provides built in support to detect the Client's Timezone and also provides necessary IO components as well as display components that transparently convert date/time data between the Server's Timezone and the Client's Timezone. What's more you can also provide the Client an option to explicitly specify a Timezone and the said wicket components will use this user supplied Timezone instead of the User's default Timezone. Here I briefly describe how you can achieve seamless conversion of date/time wicket components between server and client Timezones. The Application class We start with the Application class. In order for wicket to determine the Client's Timezone, it needs to do a small redirect to an internal page that gathers information about the Client's browser, that amongst other things includes the Client's Timezone. Note For this to work , the browser must be Javascript enabled, but even if the user's browser has disabled Javascript you can easily fallback to the Server's Timezone in such a case. import org.apache.wicket.Application; public class MyApplication extends Application { @Override public void init() { The highlighted code is where the magic happens. Well not exactly. All the "setGatherExtendedBrowserInfo(true)" call does, is instructs Wicket to temporarily redirect an incoming request to an internal Wicket page, that will gather the extended browser info. The Page classEven after setting the proper setting in the Application class, our work is not done. We need to tell wicket when to collect the browser's information. Typically to acchive a consistent look for your application, you would use markup inheritance, by having all your wicket Pages inherit from a BasePage. public class BasePage extends WebPage { // we will use this to show the client what timezone are we using for this session. private Timezone timezone ; public BasePage() { And the corresponding BasePage.html file <html> <body> In the constructor of 'BasePage' class, we trigger the redirect to the intermediate page, for obtaining the client browser's properties, using the 'getSession().getClinetInfo()' call. The Date/Time ComponentsNow for some wicket magic, after the client's Timezone has been determined, we can now use any input/label components from the wicket-datetime project, and safely rely on them to do the automatic Timezone conversions between server and client.
The DateTextField is an INPUT component which will translate the user inputted date/time values, from the client timezone to the server's timezone. Thus using wicket you can greatly simplify working with client timezones, by coding only a few lines in about 2 Java files. Other thoughtsOne thing to stress, is that wicket's ability to correctly determine the clinet's timezone depends on 2 things, one is the requirement that 'javascript' be turned on, in the client browser and secondly on the timezone offset provided by the javascript API. So it may not always work in every situation. But from my experience it works correctly most of the time. In my next post I'll examine how to convert the 'timezone' label in our example, in to a drop down select box, that will give the user an option to select the timezone he wants to see the date/time contents in. |
|||