hello.vm
结果页面 helloResult.vmInsert title here Hello Velocity
Insert title here Hello Velocity yang
#foreach ($name in $theList) Names #end $name you win
LayoutServlet
package test_vm;import java.io.BufferedWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.StringWriter;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.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.tools.view.VelocityLayoutServlet;/** * Servlet implementation class LayServlet */public class LayServlet extends VelocityLayoutServlet { private static final long serialVersionUID = 1L; /** * @see VelocityLayoutServlet#VelocityLayoutServlet() */ public LayServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.processDoc(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.processDoc(request, response); } public void processDoc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ Properties prop = new Properties(); String path = this.getServletContext().getRealPath("/"); prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path + "xa_template/"); Velocity.init(prop); VelocityContext vContext = new VelocityContext(); Template template = Velocity.getTemplate("helloResult.vm"); String p1 = request.getParameter("username"); String p2 = request.getParameter("pwd"); Vector personList = new Vector(); personList.addElement(p1); personList.addElement(p2); vContext.put("theList", personList); //将模板数据 list放置到上下文环境context中 PrintWriter writer =response.getWriter(); template.merge(vContext,writer); writer.flush(); writer.close(); }}web.xml 配置
xinai /xa_template/hello.vm velocity org.apache.velocity.tools.view.VelocityLayoutServlet velocity *.vm LayServlet LayServlet test_vm.LayServlet LayServlet /LayServlet
注意:servlet继承VelocityLayoutServlet 配置文件velocity.properties必须有如下代码片段
# Filepath for error template,# relative to web application root directorytools.view.servlet.error.template = error.vm #错误页# Directory for layout templates,# relative to web application root directorytools.view.servlet.layout.directory = xa_template/ #vm文件所在路径# Filepath of the default layout template# relative to the layout directory# NOT relative to the root directory of the webapp!tools.view.servlet.layout.default.template = hello.vm #默认页
依赖jar包
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-logging-1.1.jar
velocity-1.7-dep.jar
velocity-1.7.jar
velocity-tools-2.0.jar
velocity-tools-generic-2.0.jar
velocity-tools-view-2.0.jar