1. What is velocity
A Java EE front-end template technology , similar to jsp,freemarker , is used to display the content of the Web page . Unlike JSPs , Velocity can show only the data in the action and cannot process the data . You cannot write Java code, but you can use velocity notation. This means separating the display code from the Java code in the backend to reduce the coupling of the program.
2. Which jar packages need to be introduced
Velocity-1.5.jar,velocity-1.6.2.jar,velocity-tools-2.0.jar,velocity-tools-generic-2.0.jar, Velocity-tools-view-2.0.jar
3. Writing the template. vm files
<!--output background context settings Parameters--$name <br/><!--string-to-#foreach ($elem in $arrList) $elem < /br> #end <!--object Loops--#foreach ($elem in $userList) name: $elem. Name gender: $elem. Sex address: $elem. address</br > #end </body>
4. Writing the Servelet file
Package Com.babybus.sdteam.servelet;import Java.io.ioexception;import Java.io.printwriter;import Java.io.stringwriter;import Java.util.arraylist;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.velocitycontext;import Org.apache.velocity.app.velocity;import Org.apache.velocity.app.velocityengine;import Org.apache.velocity.exception.methodinvocationexception;import Org.apache.velocity.exception.parseerrorexception;import Org.apache.velocity.exception.resourcenotfoundexception;import Com.babybus.sdteam.vo.user;public Class Velocitytemplateservelt extends HttpServlet {private static final long serialversionuid = 1L; @Override public void DoGe T (httpservletrequest request, httpservletresponse response) throws IOException, Servletexception {//Set request and response encoded, placed garbled request.setcharacterencoding ("UTF-8 "); Response.setcontenttype ("Text/html;charset=utf-8"); Gets the page output stream PrintWriter out = Response.getwriter (); Create the properties file, or you can create properties Properties=new properties () directly under the directory; Properties.setproperty ("Resource.loader", "class"); Properties.setproperty ("Class.resource.loader.class", " Org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader "); Properties.setproperty (Velocity.encoding_default, "UTF-8"); Properties.setproperty (velocity.input_encoding, "UTF-8"); Properties.setproperty (velocity.output_encoding, "UTF-8"); Create template engine Velocityengine Velocityengine = null;try {velocityengine = new Velocityengine (properties);} catch (Except Ion e) {e.printstacktrace ();} Create context for storing variables Velocitycontext context=new velocitycontext (); Context.put ("name", "Test"); List (String) arraylist<string> arrlist = new arraylist<string> (); Arrlist.add ("test01"); Arrlist.add ("test02"); Arrlist.add ("test03"); Context.put ("Arrlist", arrlist); UserList (Storage object list) arraylist<user> userlist = new arraylist<user> (); Userlist.add (New User ("Cai", "Male", "Nanan Five Lanes")); Userlist.add (New User ("Careless", "male", "Red Light District")); Userlist.add (New User ("Forest Super", "female", "lower Third Road")); Context.put ("UserList", userlist); Read template file stream StringWriter sw = new StringWriter (); try {velocityengine.mergetemplate ("TEMPLATES/EXAMPLE.VM", "utf-8", context, SW);} catch (Resourcenotfoundexception e) {E.printstacktrace ();} catch (Parseerrorexception e) {e.printstacktrace ();} catch (Methodinvocationexception e) {e.printstacktrace ();} catch ( Exception e) {e.printstacktrace ();} Output to Page out.println (sw.tostring ()); }}
5. Configure Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name></display-name> <!-- myvelocity--> <servlet> <servlet-name>ve</servlet-name> <servlet-class> com.babybus.sdteam.servelet.velocitytemplateservelt</servlet-class> </servlet> < servlet-mapping> <servlet-name>ve</servlet-name> <url-pattern>/ve</url-pattern > </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</ Welcome-file> </welcome-file-list></web-app>
6. Enter Address: Http://localhost:8080/velocitydemo/ve Startup Project
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4827097.html
[Javaweb Basic] 019.Velocity template engine Simple example