1. First download velocity. http://velocity.apache.org/
2. Download tomcat 6.0 (windows Service installer) and install it on D: \ Program Files \ Apache Software Foundation \
3. Create a WebSite vtl in MyEclipse. The default website folder is WebRoot.
Add the following testVtlclass to WebRoot:
Package com. cl. vtl;
Import java. io. PrintWriter;
Import java. io. IOException;
Import java. util. Properties;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import org. apache. velocity. Template;
Import org. apache. velocity. VelocityContext;
Import org. apache. velocity. app. Velocity;
Import java. io. BufferedWriter;
Import java. io. OutputStreamWriter;
Public class testVtl extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Try
{
/* Properties props = new Properties ();
Props. setProperty (Velocity. INPUT_ENCODING, "GBK ");
Props. setProperty (Velocity. OUTPUT_ENCODING, "GBK ");
Props. setProperty (Velocity. RESOURCE_LOADER, "file ");
Props. setProperty ("file. resource. loader. description", "Velocity File Resource Loader ");
Props. setProperty ("file. resource. loader. class", "org. apache. velocity. runtime. resource. loader. FileResourceLoader ");
Props. setProperty ("file. resource. loader. path", "D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/vtl ");
Props. setProperty ("file. resource. loader. cache", "true ");
Props. setProperty ("file. resource. loader. modificationCheckInterval", "1 ");
Props. setProperty ("directive. foreach. counter. name", "velocityCount ");
Props. setProperty ("directive. foreach. counter. initial. value", "1 ");
*/
Properties p = new Properties ();
P. setProperty ("resource. loader", "class ");
P. setProperty ("class. resource. loader. description", "Velocity Classpath Resource Loader ");
// "Set the vm storage path, which is placed in the Classes directory. "
P. setProperty ("class. resource. loader. class", "org. apache. velocity. runtime. resource. loader. ClasspathResourceLoader ");
P. setProperty ("runtime. log. logsystem. class", "org. apache. velocity. runtime. log. NullLogSystem ");
Velocity. init (p); // initialize template settings
// Velocity. init ("velocity. properties ");
Template template = Velocity. getTemplate ("/tm/index. vm", "GBK"); // call the Template File
VelocityContext context = new VelocityContext (); // create a context Link
Context. put ("s", new String [] {"redwood", "maple", "oak", "pine "});
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (response. getOutputStream (), "GBK "));
If (context! = Null)
Template. merge (context, writer); // output
Writer. flush ();
Writer. close ();
}
Catch (Exception ex)
{
Ex. printStackTrace ();
}
}
Public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
This. doGet (req, resp );
}
}
2. web. xml configuration
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE web-app
PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Display-name> Welcome to Tomcat </display-name>
<Description>
Welcome to Tomcat
</Description>
<! -- JSPC servlet mappings start -->
<Servlet>
<Servlet-name> testVtl </servlet-name>
<Servlet-class> com. cl. vtl. testVtl </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> testVtl </servlet-name>
<Url-pattern>/index </url-pattern>
</Servlet-mapping>
<! -- JSPC servlet mappings end -->
</Web-app>
3. classpath settings
Velocity-1.6.3.jar, velocity-1.6.3-dep.jar copied to Tomcat 6.0 \ lib directory. The following are my classpath settings
.; % JAVA_HOME % \ lib \ tools. jar; % JAVA_HOME % \ lib \ dt. jar; D: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ lib \ servlet-api.jar; D: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ lib \ velocity-1.6.3.jar; D: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ lib \ velocity-1.6.3-dep.jar;
4. Create a vtl directory in the Tomcat webapps directory
Copy the program in myeclipse To The vtl directory of Tomcat 6.0 webapps.
5. Create the/tm/index. vm file in the Classes in the WEB-INF directory. This is the storage path of the template file.
<Table border = 1>
# Foreach ($ n in $ s)
<Tr> <td>
$ N is a big tree!
</Td> </tr>
# End
</Table>
6. Create a comple. bat file in the Src directory.
Write javac-d .. \ WEB-INF \ classes com \ cl \ vtl \ *. java
This bat file can be used for compilation. You can also use javac-d .. \ WEB-INF \ classes com \ cl \ vtl \ *. java in cmd (note that this command is executed in vtl/src)
7. Enter http: // loalhost: 8080/vtl/index to view the output content.
If a blank page appears in the tomcat browser, you can view the error information in the stdout. log file in the logs directory of tomcat.
Instance code
------------------------------------------------------------------
1. velocity sets the specified file location to read vm files
Properties p = new Properties ();
P. setProperty (Velocity. RESOURCE_LOADER, "file ");
P. setProperty ("file. resource. loader. description", "Velocity File Resource Loader ");
P. setProperty ("file. resource. loader. path", "D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/test ");
P. setProperty ("runtime. log. logsystem. class", "org. apache. velocity. runtime. log. NullLogSystem ");
Velocity. init (p );
2. velocity is set to read vm files in the classes file directory.
Properties p = new Properties ();
P. setProperty ("resource. loader", "class ");
P. setProperty ("class. resource. loader. description", "Velocity Classpath Resource Loader ");
P. setProperty ("class. resource. loader. class", "org. apache. velocity. runtime. resource. loader. ClasspathResourceLoader ");
P. setProperty ("runtime. log. logsystem. class", "org. apache. velocity. runtime. log. NullLogSystem ");
Velocity. init (p); // initialize template settings