LittleBill

Velocity(Java模板引擎)学习笔记

最近想了解一下Java模板语言方面的知识,发现Velocity比较火,连OSC也在用它,就用两三天时间大概学习入门下;

官网地址,官方有详细的说明,可以仔细看下。

下载地址

这里有一个新的概念,就是Velocity Template Language (VTL),官方有介绍,En不好的也可以Google一下搜索中文文档。

下图是项目结构截图:
2015-08-26_173338

我用三种形式分别介绍下用VTL生成代码方法;
(1)HelloWorld模板
(2)单对象模板
(2)对象List模板

直接上代码;

VelocityTest.java

package example;

import bean.Score;
import bean.Student;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class VelocityTest {

    public static final String HELLO_WORLD_VMS_PATH = "vms/HelloWorld.vm";
    public static final String STUDENT_VMS_PATH = "vms/Student.vm";
    public static final String SCORE_VMS_PATH = "vms/Score.vm";

    public static void main(String[] args) {
        hellWorldTP(HELLO_WORLD_VMS_PATH);
        studentTP(STUDENT_VMS_PATH);
        scoreTP(SCORE_VMS_PATH);
    }

    /**
     * 运行 对象List 模板
     * @param path
     */
    public static void scoreTP(String path) {
        VelocityEngine ve = new VelocityEngine();
        // 解决乱码问题
        ve.setProperty("input.encoding", "UTF-8");
        ve.setProperty("output.encoding", "UTF-8");
        ve.init();
        Template template =  ve.getTemplate(path);
        VelocityContext context = new VelocityContext();

        Student student = new Student();
        Score score = new Score();
        Student student1 = new Student();
        Score score1 = new Score();

        student.setName("张三");
        student.setMath(80);
        student.setChinese(75);
        student.setEnglish(70);
        score.setId(1);
        score.setStudent(student);

        student1.setName("李四");
        student1.setMath(90);
        student1.setChinese(85);
        student1.setEnglish(80);
        score1.setId(2);
        score1.setStudent(student1);

        List<Score> list = new ArrayList<Score>();
        list.add(score);
        list.add(score1);
        StringWriter writer = new StringWriter();
        context.put("list", list);
        template.merge(context, writer);
        System.out.println(writer.toString());
    }

    /**
     * 运行 单对象 模板
     * @param path
     */
    public static void studentTP(String path) {
        VelocityEngine ve = new VelocityEngine();
        // 解决乱码问题
        ve.setProperty("input.encoding", "UTF-8");
        ve.setProperty("output.encoding", "UTF-8");
        ve.init();
        Template template =  ve.getTemplate(path);
        VelocityContext context = new VelocityContext();

        Student student = new Student();
        student.setName("张三");
        student.setSex(true);
        student.setAge(20);
        student.setPhone("13612345678");
        context.put("student", student);
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        System.out.println(writer.toString());
    }

    /**
     * 运行 HelloWorld.模板
     * @param path
     */
    public static void hellWorldTP(String path) {
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        Template template =  ve.getTemplate(path);
        VelocityContext context = new VelocityContext();

        context.put("hello", "HelloWorld");
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        System.out.println(writer.toString());
    }
}

Student.java

package bean;

public class Student {
    private String name;
    private boolean sex;
    private int age;
    private String phone;
    private int math;
    private int chinese;
    private int english;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }
}

Score.java

public class Score {
    private int id;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    private Student student;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

HelloWorld.vm

==============
Template one:
==============

##hello world!
$hello

Student.vm

==============
Template two:
==============
## Student info
姓    名: $student.name

性    别:#if( $student.sex ) 男 #else 女 #end


年    龄: $student.age

手机号码: $student.phone

Score.vm

==============
Template three:
==============
## Score info

#foreach($element in $list)
$element.id, $element.student.name, $element.student.math, $element.student.chinese, $element.student.english
#end


执行结果
2015-08-26_173455

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注