You can easily use Velocity templates by adding the following servlet filter to your web.xml file. After that you can start forwarding and redirecting your actions to velocity templates (*.vm files).
Adding the velocity filter to the web.xml:
<!-- Define the velocity filter --> <filter> <filter-name>VelocityFilter</filter-name> <filter-class> org.mentawai.velocity.VelocityServletFilter </filter-class> </filter> <!-- Map the filter to a file extension --> <filter-mapping> <filter-name>VelocityFilter</filter-name> <url-pattern>*.vm</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
Using any Menta Tag inside a velocity template:
To call any Menta Tag from inside a velocity template, add the following servlet to your web.xml:
<servlet> <servlet-name>tags-for-vm</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>tags-for-vm</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping>
Now you can mix both velocity directives and menta tags in the same template:
<%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%> <!-- example of a hello.vm file --> <html> <body> <!-- mentawai tag --> <h3>Hello <mtw:out value="username" /> from Mentawai!</h3> <!-- now velocity --> <h3>Hello $username with Velocity!</h3> <!-- do something crazy with velocity --> #set( $criteria = ["name", "address"] ) #foreach( $criterion in $criteria ) <h1>$criterion</h1> #end </body> </html>
Calling velocity from a JSP file:
You can also invert and use velocity from inside a JSP file. To do that you can use the mtw:velocity tag:
<%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%> <!-- example of a hello.jsp file --> <html> <body> <!-- mentawai tag --> <h3>Hello <mtw:out value="username" /> from Mentawai!</h3> <!-- now velocity --> <mtw:velocity> <h3>Hello $username with Velocity!</h3> <!-- do something crazy with velocity --> #set( $criteria = ["name", "address"] ) #foreach( $criterion in $criteria ) <h1>$criterion</h1> #end </mtw:velocity> </body> </html>