Wednesday, December 5, 2007

How to use Velocity as a view component in Struts?

Struts provides a proved framework for building web applications in Java. It is most often used with JSP as a view component. However, other view components may be used as well.

Here I will show how to create a "hello world" application using Struts and Velocity as a view component. I assume you have already downloaded and set up necessary Struts parts.

First of all, we need to download VelocityTools package which provides easy way to use "*.vm" (Velocity Macro) templates to serve HTML pages and has some tools for Struts integration.

Extract the archive and copy following jar files to your project:
  • velocity-tools-1.4.jar
  • velocity-1.5.jar
  • commons-beanutils-1.7.0.jar
  • commons-logging-1.1.jar
  • commons-collections-3.2.jar
  • oro-2.0.8.jar
  • commons-digester-1.8.jar
  • commons-lang-2.2.jar

Add following servlet definition and mapping to your web.xml:

Now create a hello-world.vm file in the root directory of your web resources, where you would normally put JSP files.
Hello World at $currentTime!

If you start application and point your browser to the http://localhost:8080/struts-velocity/hello-world.vm URL, you will see following output:
  Hello World at $currentTime!    

Create HelloWorldAction Struts Action to add necessary "currentTime" object to the request.

In struts-config.xml:
  <action path="/hello-world" type="vilbara.HelloWorldAction">
<forward name="ok" path="/hello-world.vm"/>

In HelloWorldAction class:
  request.setAttribute("currentTime", new Date());
return actionMapping.findForward("ok");

Start application and access http://localhost:8080/struts-velocity/ URL. You will get following output:
  Hello World at Thu Dec 06 12:36:33 CET 2007!

That was it. Now Struts uses Velocity for the view component in your application.

You may download struts-velocity.war file. Sources are included.

Use Velocity for e-mail templates

You are Java developer and need to send an e-mail. Where and how do you store e-mail content?

First, let me clarify what I mean by the "content". There are several parts of it:
  • subject.
  • body
  • "from" field.
Many developers use Java property resource bundles and naming convention to store 2 or 3 parts of content.
  mail.created.subject=Account for {0} has been created.
mail.created.body=Account with the user ID {0} has been successfully created.\n\n\
Please follow this link to use your account:\n {1}\n\nYour Support Team\
Let's look at the alternate solution using Apache's Velocity.

First, create a file in Velocity macro, e.g. mail-created.vm with following content:
  #set ($subject = "Account for $ has been created")
#set ($from = '')

Account with the user ID $person.uid has been successfully created.

Please follow this link to use your account:

Your Support Team
Now, load and evaluate the template:
  Context context = new VelocityContext();
context.put("person", new Person("Vilmantas Baranauskas", "vilbara"));
context.put("link", "");

Template template = Velocity.getTemplate("src/mail-created.vm");

StringWriter body = new StringWriter();
template.merge(context, body);

String from = String.valueOf(context.get("from"));
String subject = String.valueOf(context.get("subject"));

Compare abilities of Velocity and MessageFormat and you will see the benefits.

You may download a zip file containing this example.

Tuesday, December 4, 2007

How to upgrade HDD without reinstalling Windows?

Have you run out of space on your hard disk drive? Your HDD is aging and you are afraid of it breaking soon and possibly loosing some data? Are you procrastinating to upgrade it because of time consuming installations?

I do upgrade my hard disk drives every 3 years or so, before they die unexpectedly or I run out of space. And this is not time consuming at all. Here is what I do.
  1. Buy new HDD. I usually buy some of the cheapest models available. If you buy every 3 years then you have a good chance that your new HDD will be twice as big as your current one.
  2. Download and burn Linux live-CD, e.g. Ubuntu
  3. Attach your new HDD to the computer. You may use USB HDD enclosure for notebooks to attach new HDD using USB port.
  4. Boot Linux live-CD.
  5. Start command line shell. Use menu "Applications/Accesories/Terminal" in Ubuntu.
  6. Make sure you see both disks:
    sudo fdisk -l
    It is likely that your old disk is available as "/dev/sda" and your new one is available as "/dev/sdb".
  7. Copy complete content of your old disk, to the new one.
    sudo cp /dev/sda /dev/sdb
  8. Resize partition and file system or create new partition.
    sudo parted /dev/sdb
Now you are ready to shut down the computer, remove old disk and install the new one. Start your system and enjoy your new and bigger (and likely faster) hard disk.

The last step? Erase data on your old HDD and sell it on eBay.

Forget time-consuming reinstalls.