Showing posts with label velocity. Show all posts
Showing posts with label velocity. Show all posts

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:
  <servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
Now create a hello-world.vm file in the root directory of your web resources, where you would normally put JSP files.
  <html>
<body>
Hello World at $currentTime!
</body>
</html>

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"/>
</action>

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

Start application and access http://localhost:8080/struts-velocity/hello-world.do 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.from=support@example.org
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\nexample.org
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 $person.name has been created")
#set ($from = 'support@example.org')

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

Please follow this link to use your account:
$link

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

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.