velocity基本使用

1. Velocity 开发

apache Velocity开发指导传送门

1.1 Velocity使用流程

  • 初始化Velocity(单例或者多实例)
  • 创建一个context
  • 向context中添加对象
  • 选择模版
  • Merge 模版和context

1.2 pom依赖

<!-- velocity -->
<dependency>    
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>    
  <version>1.7</version>
</dependency>

1.3不同模式下使用velocity

  • 单例模式使用: org.apache.velocity.app.Velocity
  • 多实例模式使用: org.apache.velocity.app.VelocityEngine

1.3.1 单例模式使用velocity

//初始化Velocity
Velocity.init();
//创建context
VelocityContext context = new VelocityContext();
//添加数据
context.put( "name", new String("Velocity") );
//获取模版
Template template = Velocity.getTemplate("templates/helloworld.vm");
//Merge 模版和context
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());

1.3.2 多实例使用velocity

//每次创建VelocityEngine对象
VelocityEngine ve = new VelocityEngine();
ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
ve.init();
//创建context
VelocityContext context = new VelocityContext();
//添加数据
context.put( "name", new String("Velocity") );
//获取模版
Template template = Velocity.getTemplate("templates/helloworld.vm");
//Merge 模版和context
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());

1.4 Context

1.4.1 Context 基本概念

Context是作为数据容器的,实现是基于HashMap的,模版中用到的变量都必须放到context中,类似于spring中的ModelAndView。没有特殊需求的话可以使用官方推荐的VelocityContext,不支持jdk对象序列化。
VelocityContext构造器默认实现HashMap:

    /**
     *  Initializes internal storage (never to <code>null</code>), and
     *  inner context.
     *
     *  @param context Internal storage, or <code>null</code> to
     *  create default storage.
     *  @param innerContext Inner context.
     */
    public VelocityContext(Map context, Context innerContext)
    {
        super(innerContext);
        //如果conext 为空新建一个
        this.context = (context == null ? new HashMap() : context);
    }
```
context的添加获取操作:
```
    /**
     * Adds a name/value pair to the context.
     *
     * @param key   The name to key the provided value with.
     * @param value The corresponding value.
     * @return The old object or null if there was no old object.
     */
    Object put(String key, Object value);

    /**
     * Gets the value corresponding to the provided key from the context.
     *
     * @param key The name of the desired value.
     * @return    The value corresponding to the provided key.
     */
    Object get(String key);
```
### 1.4.2.1 #foreach( ) 支持的数据结构
- 实现Iterable接口的对象
- 数组会被velocity包装成实现Iterable的类,v1.6之后可以把数组当成list来看待,可以使用size(), isEmpty() and get(int)方法。
- Map values()方法需要返回有用的信息
- Enumeration只能使用一次,没有复位会报错

### 1.4.2.2 支持static类
不是所有的类都可以创建对象,比如Math类没有公有的构造器,要用到Math的方法可以直接把math的class对象传过去。
```
context.put("Math", Math.class);
```
## 1.4.2.3 context嵌套
```
VelocityContext context1 = new VelocityContext();

context1.put("name","Velocity");
context1.put("project", "Jakarta");
context1.put("duplicate", "I am in context1");

VelocityContext context2 = new VelocityContext( context1 );

context2.put("lang", "Java" );
context2.put("duplicate", "I am in context2");

template.merge( context2, writer );
```
- 如果有重复的key最后实际上存的是最后一个key的值,上面代码中get duplicate会得到I am in context2
- 重复的key不会影响原有的context中的值,context1.get("duplicate")获取到的还是I am in context1
- #set()可以影响到外层的context的值,但是不影响内层的

















最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容