美文网首页
velocity基本使用

velocity基本使用

作者: 七海的游风 | 来源:发表于2017-05-22 15:40 被阅读1277次

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的值,但是不影响内层的

















相关文章

  • velocity基本使用

    1. Velocity 开发 apache Velocity开发指导传送门 1.1 Velocity使用流程 初始...

  • Velocity的使用小记

    Velocity文件后缀为vm,以下简称为vm。 几个基本使用 1.取值:$!url, $url, $!{url}...

  • velocity基本语法

    1.赋值 #set ( $foo = “Velocity” ) Hello $foo World! 2.注释 ##...

  • 使用velocity

    1.是什么 velocity是一个基于java的模板引擎,提供了一个Context容器,在java代码中我们可以往...

  • 项目实践技术点积累

    velocity 标签内使用判断语句,修改标签属性 使用模板布局,并传输参数 让velocity解析url上的参数...

  • Velocity 基本模板语言语法使用

    在 hellovelocity.vm 里面可以看到很多以 # 和$符开头的内容,这些都是 Velocity 的语法...

  • 对管理者解释敏捷度量

    敏捷里经常统计Velocity、控制图、燃尽图、累计流图,这四个图表作为敏捷度量最基本的统计方式被大量使用...

  • velocity学习

    velocity模板引擎 velocity是一个基于Java的模板引擎。它能够做到在前端页面使用模板语言来引用Ja...

  • 16.vue中Js动画与Velocity.js库结合

    Js钩子动画 Velocity.js库和js钩子结合使用

  • 扩展阅读

    thymeleafRedis教程JPA使用指南了解Groovy,kotlin,scala, Velocity 语言

网友评论

      本文标题:velocity基本使用

      本文链接:https://www.haomeiwen.com/subject/scfputtx.html