How to configure Eclipse, Tomact 5.5, velocity

.


It took me a whole day to get my head around the configuration of Tomcat with latest velocity package

So I thought why not i should share it with the world.

Prerequisites
Tomcat 5.5
Velocity
Velocity Tools
Eclipse
Sysdeo Tomcat launcher plugin
Lots of Patience

Process

After downloading the above mentioned packages follow the below mentioned steps

Step 1: Setup Eclipse and Tomcat

Unzip the sysdeo plugin and copy it into plugin folder of the eclipse
Restart the Eclipse

Some new Menu button will be added in the Eclipse



  • Click on windows menu of Eclipse->Preference
  • Click on Tomcat on the left hand listbox
  • Click in Tomcat 5.X radio button
  • Browse the Tomcat home i.e where Tomcat is installed.
  • Select Context radio button in Context declaration mode (This will allow web application to declare resource in their own context.xml file)
  • Click Apply

Step 2 : Creating a Tomcat Project

Go to Menu of Eclipse follow the instructions below

File-->New-->Projects-->Tomcat Project

Due to the sysdeo plugin web directory structure with necessary jars will be creates

Step 3 : Setting up Velocity 1.6.x and Velocity Tools

  • Extract the downloaded velocity package in the folder you like
  • Right Click on Referenced Libraries in Eclipse
  • Click on Build Path-->Configure Build Path menu options
  • Click on External Jars button and add velocity-1.6.2, velocity-1.6.3-dep.jar, Velocity-tools-1.4.jar, Velocity-tools-view-1.4.jar

This will include the jars in build path of web application.


Step 4 : Writing some code

Create a any class under WEB-INF/src directory in eclipse
Below is the test class I have used
_____________________________________________________________________________________

public class SimpleServlet
{
private String message = "Hello from ToyTool!";

public String getMessage()
{
return message+" "+foo;

}

public void setMessage(String m)
{
message = m;
}

/** To test exception handling in templates. */
public boolean whine() {
throw new IllegalArgumentException();
}

}
__________________________________________________________________________________


Step 5 : Setting up necessary xml files

There are three file web.xml, toolbox.xml, velocity.properties which are important at the moment.

Create three empty file name web.xml, velocity.properties, toolbox.xml under WEB-INF directory

Open web.xml

Copy and paste below code mentioned
________________________________________________________________________________
<?xml version='1.0' encoding='utf-8'?>
<web-app>

<!-- Define Velocity template compiler -->
<servlet>
<servlet-name>serv</servlet-name>

<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet </servlet-class>



<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>

<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>

</servlet>

<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>serv</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>

</web-app>
______________________________________________________________________________

Open toolbox.xml

Copy and paste the code below mentioned
________________________________________________________________________________
<?xml version="1.0"?>

<toolbox>
<xhtml>true</xhtml>
<tool>
<key>serv</key>
<scope>request</scope>
<request-path>index.vm</request-path>
<class>SimpleServlet</class>
</tool>
<data type="number">
<key>version</key>
<value>1.1</value>
</data>
<data type="boolean">
<key>isSimple</key>
<value>true</value>
</data>
<data type="string">
<key>foo</key>
<value>this is foo.</value>
</data>
<data type="string">
<key>bar</key>
<value>this is bar from velocity.</value>
</data>
<tool>
<key>map</key>
<scope>session</scope>
<class>java.util.HashMap</class>
</tool>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
</toolbox>
__________________________________________________________________________________

Open velocity.properties

Copy ans paste the code below mentioned
________________________________________________________________________________

webapp.resource.loader.path=/WEB-INF/templates/

_______________________________________________________________________________

Create a folder name called "templates" under WEB-INF directory
template folder will contain all our velocity templates

Create a index.vm file under template directory which will serve as template for the respective servlet

Copy and paste the code in the index.vm
_______________________________________________________________________

<html>
<body>
The Geek Head $serv.getMessage()
</body>
<html>
_______________________________________________________________________
____


Step 6 : Testing the above configuration

Time to check weather velocity is configured properly
Click on Tomcat menu-->stat Tomcat
And test the following url http://localhost:8080/Tomcattest/index.vm
where the Tomcattest is the project name

For further documentations please visit respective package's website.



0 comments:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment