velocity-1.5.jar(382.9 K) | |
velocity-dep-1.5.jar(682.2 K) | |
首先创建一个vtl项目,在项目下生成一个servlet
testVtl.java:
1 |
package com.cl.vtl; 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; /** * @version 1.0 * @author */ public class testVtl extends HttpServlet { /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ 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 5.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"); Velocity.init(props);//初始化模板设置 //Velocity.init("velocity.properties"); Template template = Velocity.getTemplate("/tm/index.vm", "GBK");//调用模板文件 VelocityContext context = new VelocityContext();//建立上下文链接 context.put("s", new String [] { "redwood", "maple", "oak", "pine" }); BufferedWriter writer = writer = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"GBK")); if ( context != null) template.merge(context, writer);//输出 writer.flush(); writer.close(); }catch(Exception ex){ex.printStackTrace();} } /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ this.doGet(req,resp) ; } } |
web.xml:
1 |
<?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>org.apache.jsp.index_jsp</servlet-name> <servlet-class>org.apache.jsp.index_jsp</servlet-class> </servlet> <servlet-mapping> <servlet-name>org.apache.jsp.index_jsp</servlet-name> <url-pattern>/index.jsp</url-pattern> </servlet-mapping> <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> |
/*在D:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/vtl下建立tm文件夹
tm文件夹下存放模板文件*/
建立模板文件index.vm
index.vm:
1 |
<table border=1> #foreach($n in $s) <tr><td> $n is a big tree! </td></tr> #end </table> |
**注:将附件的两个jar包放到项目的lib文件夹下