一个关于Velocity的例子

自己做的一个关于velocity的例子。

 

index.vm

---------------------------------

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Velocity Demo</title>   
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="keywords" content="velocity,模板">
 </head>
    <body >       
        #set($name1="first!")
        Hello, $name1  <br>
        Hello, $name2
        <br/>
        Hello, $name3
         <br/>
        Hello, $name3
        <hr>
       
        <table border='1' width='200' >
        <tr>
        <td>
         yy
        </td>
        </tr>
        #foreach ($iii in $theList)
        <tr>
          <td bgcolor="#eeeeee">$iii</td>
        </tr>
        #end
        </table>
    </body>
</html>


************************

 

MyVelocityServlet.java

-----------------------------------------------------------------------

package com.velocity;

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.VelocityContext;

@SuppressWarnings("deprecation")
public class MyVelocityServlet extends VelocityServlet {

    protected Properties loadConfiguration(ServletConfig config)
            throws IOException, FileNotFoundException {

        VelocityEngine engine = new VelocityEngine();

        Properties p = new Properties();

        String path = config.getServletContext().getRealPath("/");

        if (path == null) {
            path = "/";
        }

        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);

        p.setProperty("runtime.log", path + "velocity.log");
       

        try {
            engine.init(p); // 载入模板的路径path ,即上下文路径
        } catch (Exception e) {          
            e.printStackTrace();
        }

        return p;
    }

    public Template handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context ctx) {
     
     try {
   request.setCharacterEncoding("UTF8");
  } catch (UnsupportedEncodingException e1) {
   
   e1.printStackTrace();
  }
  
     response.setCharacterEncoding("UTF8");
        Template template = null;
        Template template2 = null;

        try {
            /**
             * 主要代码
             */
            Velocity.init();
          
           // VelocityContext context = new VelocityContext();

            String p1 = "JAVA";
            String p2 = "C++";
            String p3 = "Ruby";
            String p4 = "D";
            Vector<string></string> personList = new Vector<string></string>();
            personList.addElement(p1);
            personList.addElement(p2);
            personList.addElement(p3);
            personList.addElement(p4);

            /**
             * 将模板数据name, list 放置到上下文环境 context 中去
             */
            ctx.put("name2", " 这里是在后台赋值! ");
            ctx.put("name3", " 小齐! ");
            ctx.put("theList", personList);

            template = Velocity.getTemplate("/index.vm");

        } catch (Exception e) {
            e.printStackTrace();
        }
       
        // 以下一段代码主要是获得模板的HTML内容 在后台显示
        try {

            template2 = Velocity.getTemplate("/index.vm");

            VelocityContext context = new VelocityContext();

            context.put("name2", "这里在后台第二次赋值!");

            StringWriter writer = new StringWriter();

            template2.merge(context, writer);

            System.out.println(writer.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return template;
    }
}

***********************************

本例子是参照网上搜集的资料而形成的,现提出来供大家参考,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值