博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Velocity初尝试
阅读量:6431 次
发布时间:2019-06-23

本文共 4594 字,大约阅读时间需要 15 分钟。

  hot3.png

hello.vm

    
Insert title here

Hello Velocity

结果页面 helloResult.vm

    		
Insert title here

Hello Velocity yang

#foreach ($name in $theList)
#end
Names
$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

转载于:https://my.oschina.net/UpBoy/blog/155572

你可能感兴趣的文章
[ZHUAN]Flask学习记录之Flask-SQLAlchemy
查看>>
【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint
查看>>
PNG怎么转换成32位的BMP保持透明
查看>>
经验分享:CSS浮动(float,clear)通俗讲解
查看>>
WTL中最简单的实现窗口拖动的方法(转)
查看>>
数据结构—队列
查看>>
BZOJ4241 : 历史研究
查看>>
(LeetCode)两个队列来实现一个栈
查看>>
[WebGL入门]十九,遮挡剔除和深度測试
查看>>
jquery封装常用方法
查看>>
什么是ICE (Internet Communications Engine)
查看>>
移动web开发之屏幕三要素
查看>>
求按小时统计的语句,该怎么处理
查看>>
TRUNCATE,DORP,DELETE
查看>>
Chrome的开发必备小技巧
查看>>
can-i-win(好)
查看>>
Centos6.5下安装protobuf及简单使用
查看>>
[SharePoint] SharePoint 错误集 3
查看>>
高压光耦
查看>>
[转]DPM2012系列之六:在Win7上安装DPM远程管理控制台
查看>>