Development environment
Eclipse Java EE IDE for WEB developers. (Version:helios Service Release 1)
Jdk1.6.0_07
apache-tomcat-6.0.10
First you need to install the Velocity Editor plugin for Eclipse:
In Eclipse's Help->install New software ...
Click "Add plus veloeclipse, value: http://veloeclipse.googlecode.com/svn/trunk/update/
Note: If unable to install, remove the Group Items by catagory before installing the plugin interface
Create a project
Open Eclipse,file, New and other ... web projec after you follow the prompts .
Note 1: Modify the code file output path, default is build\classes; this does not meet the requirements of Tomcat and needs to be changed to Webcontent\web-inf\classes as follows:
NOTE 2: Tick Generate Web. XML Deployment Descriptor
Adding a velocity dependency package
Velocity-related packages have more than 10, download unzip after copy all, paste into project WebContent-Web-inf->lib folder
Project Name Right-click menu Select Build path-Configure build Path->java build path->libraries, add jars Select Project Lib folder, select all Jar last point confirm press Button
Note: The jar package required by velocity http://download.csdn.net/detail/smilevt/4802244
add a velocity template file
Add a folder under WebContent, my name is templates. Add a HELLO.VM file under the file with the following contents:
<body bgcolor= "#ffffff" >
<center>
<table width= "cellpadding=" 5 "cellspacing=" 1 "bordercolor=" #333333 ">
<TR><TD bgcolor= "#eeeeee" align= "center" >name list</td></tr>
#foreach ($name in $theList)
<TR><TD bgcolor= "#6666FF" align= "center" > $name </td></tr>
#end
</table>
</center>
</body>
adding Java class files
Create a new package under the Java Resource---SRC, the package name Com.velocitydemo.velocityhandler, the name is arbitrary, in the following configuration file to use, need to be consistent before and after
Add a class under the package, the class name Hellohandler, the name is arbitrary, the same reason
The code for the class is as follows:
Package Com.velocitydemo.velocityhandler;
Import java.util.Properties;
Import Java.util.Vector;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.velocity.Template;
Import org.apache.velocity.app.Velocity;
Import Org.apache.velocity.app.VelocityEngine;
Import Org.apache.velocity.context.Context;
Import Org.apache.velocity.tools.view.VelocityViewServlet;
public class Hellohandler extends Velocityviewservlet {
Private static final long serialversionuid = 1L;
Private Velocityengine Velo;
public void Init () throws Servletexception {
Velo = new Velocityengine ();//Velocity Engine Object
Properties prop = new properties ();//Set the mount path of the VM template
String path = This.getservletcontext (). Getrealpath ("/");
Prop.setproperty (Velocity.file_resource_loader_path, PATH + "templates/");
try {
Velo.init (prop);//Initialize settings, use GetTemplate ("*.VM") output below, make sure to invoke Velo object to do, i.e. velo.gettemplate ("*.VM")
} catch (Exception E1) {
E1.printstacktrace ();
}
}
Protected Template HandleRequest (httpservletrequest request, httpservletresponse response, Context ctx) {
String p1 = "Charles";
String P2 = "Michael";
Vector personlist = new vector ();
Personlist.addelement (p1);
Personlist.addelement (p2);
Ctx.put ("Thelist", personlist); To place the template data list in context contexts
Template template = Velo.gettemplate ("HELLO.VM");
return template;
}
}
Configure Web. xml
Open the Web. xml file under Web-inf, with the following content:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
&NBSP;&NBSP;&NB Sp xmlns= "Http://java.sun.com/xml/ns/javaee"
xmlns:web= " Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
& nbsp; xsi:schemalocation= "http://java.sun.com/xml/ Ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
id= "webapp_id" version= "3.0" >< /p>
<display-name>velocity-demo</display-name>
<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>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.velocitydemo.velocityhandler.HelloHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/hello</welcome-file>
</welcome-file-list>
</web-app>
at this point one of the simplest velocity projects has been created
Note: The Classes folder under Web-inf is not displayed in the project map
Publish Project
Here you can publish the WebContent in the project folder to Tomcat for testing.
method of publication reference address (http://blog.csdn.net/smilevt/article/details/8212075)
start Tomcat to access one of the simplest velocity projects
Eclipse Development Velocity Example (beginner)