Download velocity-tools-2.0-src
Add the following jars in WEB-INF/lib folder of tomcat
velocity-1.6.2.jar
velocity-tools-view-2.0.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-lang-2.2.jar
commons-logging-1.1.jar
oro-2.0.8.jar
Add the following jars in WEB-INF/lib folder of tomcat
velocity-1.6.2.jar
velocity-tools-view-2.0.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-lang-2.2.jar
commons-logging-1.1.jar
oro-2.0.8.jar
Create a project simple inside webapps using eclipse
1.Inside simple folder create index.jsp
<%@taglib prefix="velocity" uri="http://velocity.apache.org/velocity-view" %>
<html>
<body>
I'm a JSP file that uses the VelocityViewTag.
<velocity:view>
#if( $XHTML )
#set( $br = "<br />" )
#else
#set( $br = "<br>" )
#end
$br
$br
Here we use a custom tool: $toytool.message
$br
$br
Lets count : #foreach($i in [1..5])$i #end
$br
$br
Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map
$br
$br
Here we get the date from the DateTool: $date.medium
$br
$br
#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end
$br
$br
Click <a href="index.vm">here</a> to see this VTL markup as a normal template.
</velocity:view>
</body>
</html>
<html>
<body>
I'm a JSP file that uses the VelocityViewTag.
<velocity:view>
#if( $XHTML )
#set( $br = "<br />" )
#else
#set( $br = "<br>" )
#end
$br
$br
Here we use a custom tool: $toytool.message
$br
$br
Lets count : #foreach($i in [1..5])$i #end
$br
$br
Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map
$br
$br
Here we get the date from the DateTool: $date.medium
$br
$br
#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end
$br
$br
Click <a href="index.vm">here</a> to see this VTL markup as a normal template.
</velocity:view>
</body>
</html>
2. Create index.vm file inside the simple folder
<html>
<body>
I'm a velocity template processed using the VelocityViewServlet.
#if( $XHTML )
#set( $br = "<br />" )
#else
#set( $br = "<br>" )
#end
$br
$br
Here we use a custom tool: $toytool.message
$br
$br
Lets count : #foreach($i in [1..5])$i #end
$br
$br
Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map
$br
$br
Here we get the date from the DateTool: $date.medium
$br
$br
#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end
$br
$br
Click <a href="index.jsp">here</a> to see the VelocityViewTag handle the same VTL markup.
</body>
</html>
3. In src under WEB-INF folder place class file
public class ToyTool
{
private String message = "Hello from ToyTool!";
public String getMessage()
{
return message;
}
public void setMessage(String m)
{
message = m;
}
/** To test exception handling in templates. */
public boolean whine() {
throw new IllegalArgumentException();
}
}
4. Web.xml
<web-app>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>
5. Beside web.xml create a file tools.xml
<?xml version="1.0"?>
<tools>
<data type="boolean" key="xhtml" value="true"/>
<data type="boolean" key="isSimple" value="true"/>
<data type="number" key="version" value="2.0"/>
<data key="foo">this is foo</data>
<data key="bar">this is bar.</data>
<toolbox scope="request">
<tool key="toytool" class="ToyTool" restrictTo="index*"/>
</toolbox>
<toolbox scope="session">
<tool key="map" class="java.util.HashMap"/>
</toolbox>
</tools>
<tools>
<data type="boolean" key="xhtml" value="true"/>
<data type="boolean" key="isSimple" value="true"/>
<data type="number" key="version" value="2.0"/>
<data key="foo">this is foo</data>
<data key="bar">this is bar.</data>
<toolbox scope="request">
<tool key="toytool" class="ToyTool" restrictTo="index*"/>
</toolbox>
<toolbox scope="session">
<tool key="map" class="java.util.HashMap"/>
</toolbox>
</tools>
In lib folder place the jar files mentioned above...start tomcat and try accessing the index.jsp
So the folder structure is
Simple
|
|WEB-INF
| | src
| | lib
| | tools.xml
| | web.xml
| index.jsp
| index.vm
Simple
|
|WEB-INF
| | src
| | lib
| | tools.xml
| | web.xml
| index.jsp
| index.vm
If you want a working example send me a mail at harshal.shah1982@gmail.com
No comments:
Post a Comment