apache的开源项目-模板引擎(Velocity)_学习了两天就上手啦_源码下载

首先,如果你对Velocity不是很了解,还是建议你去apache的官方网站上去走走....

这是velocity的官网:http://velocity.apache.org/

当然如果你对英文文档不是很感冒,这里也有好的资料:

Velocity 文档(1) Velocity 文档(2) Velocity 文档(3)

下面我就正式说说我做的项目啦...

项目结构:

运行"helloWorld.vm"模板效果:

运行"userInfo.vm"模板效果:

运行"emailTemplate.vm"模板效果:

==============================================================

代码部分:

==============================================================

/Apache-Velocity-java/src/com/b510/velocity/test/VelocityTest.java

  1 /**
  2  * 
  3  */
  4 package com.b510.velocity.test;
  5 
  6 import java.io.StringWriter;
  7 import java.text.SimpleDateFormat;
  8 import java.util.Date;
  9 
 10 import org.apache.velocity.Template;
 11 import org.apache.velocity.VelocityContext;
 12 import org.apache.velocity.app.VelocityEngine;
 13 
 14 import com.b510.velocity.bean.Mail;
 15 import com.b510.velocity.bean.User;
 16 
 17 /**
 18  * 测试类
 19  * 
 20  * @author hongten<br>
 21  * @date 2013-3-9
 22  */
 23 public class VelocityTest {
 24 
 25     public static final String HELLO_WORLD_VM_PATH = "vms/helloWorld.vm";
 26     public static final String USER_INFO_VM_PATH = "vms/userInfo.vm";
 27     public static final String EMAIL_TEMPLATE_VM_PATH = "vms/emailTemplate.vm";
 28 
 29     public static void main(String[] args) throws Exception {
 30         sayHelloFromVM(HELLO_WORLD_VM_PATH);
 31         testUser(USER_INFO_VM_PATH);
 32         testEmail(EMAIL_TEMPLATE_VM_PATH);
 33     }
 34 
 35     /**
 36      * 简单的hello world
 37      * 
 38      * @param fileVM
 39      * @throws Exception
 40      */
 41     public static void sayHelloFromVM(String fileVM) throws Exception {
 42         VelocityEngine ve = new VelocityEngine();
 43         ve.init();
 44         Template t = ve.getTemplate(fileVM);
 45         VelocityContext context = new VelocityContext();
 46         context.put("hello", "Hello world!!");
 47         StringWriter writer = new StringWriter();
 48         t.merge(context, writer);
 49         System.out.println(writer.toString());
 50     }
 51 
 52     /**
 53      * test User
 54      * 
 55      * @param fileVM
 56      * @throws Exception
 57      */
 58     public static void testUser(String fileVM) throws Exception {
 59         VelocityEngine ve = new VelocityEngine();
 60         ve.init();
 61 
 62         Template template = ve.getTemplate(fileVM);
 63         VelocityContext velocityContext = new VelocityContext();
 64         User user = new User();
 65         user.setEmail("hongtenzone@foxmail.com");
 66         user.setName("hongten");
 67         user.setBirthday("1990-11-18");
 68         velocityContext.put("user", user);
 69         StringWriter stringWriter = new StringWriter();
 70         template.merge(velocityContext, stringWriter);
 71 
 72         System.out.println(stringWriter.toString());
 73     }
 74 
 75     /**
 76      * test email
 77      * 
 78      * @param fileVM
 79      * @throws Exception
 80      */
 81     public static void testEmail(String fileVM) throws Exception {
 82         VelocityEngine velocityEngine = new VelocityEngine();
 83         velocityEngine.init();
 84 
 85         Template template = velocityEngine.getTemplate(fileVM);
 86         VelocityContext velocityContext = new VelocityContext();
 87         Mail mail = new Mail();
 88         mail.setContent("2013年腾讯开发者新扶持政策解读及创业机会所在");
 89         mail.setReceiverMail("hongtenzone@foxmail.com");
 90         mail.setReceiverName("Hongten");
 91         mail.setSenderMail("opensns_noreply@tencent.com");
 92         mail.setSenderName("腾讯开放平台");
 93         mail.setSenderWebSite("open.qq.com");
 94         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
 95                 "yyyy-MM-dd HH:mm:ss");
 96         mail.setDate(simpleDateFormat.format(new Date()));
 97         velocityContext.put("mail", mail);
 98         StringWriter stringWriter = new StringWriter();
 99         template.merge(velocityContext, stringWriter);
100 
101         System.out.println(stringWriter.toString());
102     }
103 }

/Apache-Velocity-java/src/com/b510/velocity/bean/User.java

 1 /**
 2  * 
 3  */
 4 package com.b510.velocity.bean;
 5 
 6 
 7 /**
 8  * 用户实体类
 9  * 
10  * @author hongten<br>
11  * @date 2013-3-9
12  */
13 public class User {
14 
15     /**
16      * 用户编号
17      */
18     private Integer id;
19     /**
20      * 用户名称
21      */
22     private String name;
23     /**
24      * 密码
25      */
26     private String password;
27     /**
28      * 生日
29      */
30     private String birthday;
31     /**
32      * 邮箱
33      */
34     private String email;
35 
36     public Integer getId() {
37         return id;
38     }
39 
40     public void setId(Integer id) {
41         this.id = id;
42     }
43 
44     public String getName() {
45         return name;
46     }
47 
48     public void setName(String name) {
49         this.name = name;
50     }
51 
52     public String getPassword() {
53         return password;
54     }
55 
56     public void setPassword(String password) {
57         this.password = password;
58     }
59 
60     public String getBirthday() {
61         return birthday;
62     }
63 
64     public void setBirthday(String birthday) {
65         this.birthday = birthday;
66     }
67 
68     public String getEmail() {
69         return email;
70     }
71 
72     public void setEmail(String email) {
73         this.email = email;
74     }
75 
76 }

/Apache-Velocity-java/src/com/b510/velocity/bean/Mail.java

  1 /**
  2  * 
  3  */
  4 package com.b510.velocity.bean;
  5 
  6 /**
  7  * 邮件
  8  * 
  9  * @author hongten<br>
 10  * @date 2013-3-9
 11  */
 12 public class Mail {
 13 
 14     private Integer id;
 15     /**
 16      * 发件人
 17      */
 18     private String senderName;
 19     /**
 20      * 发件人邮箱
 21      */
 22     private String senderMail;
 23     /**
 24      * 发件人网址
 25      */
 26     private String senderWebSite;
 27     /**
 28      * 收件人
 29      */
 30     private String receiverName;
 31     /**
 32      * 收件人邮箱
 33      */
 34     private String receiverMail;
 35     /**
 36      * 内容
 37      */
 38     private String content;
 39     /**
 40      * 日期
 41      */
 42     private String date;
 43 
 44     public Integer getId() {
 45         return id;
 46     }
 47 
 48     public void setId(Integer id) {
 49         this.id = id;
 50     }
 51 
 52     public String getSenderName() {
 53         return senderName;
 54     }
 55 
 56     public void setSenderName(String senderName) {
 57         this.senderName = senderName;
 58     }
 59 
 60     public String getSenderMail() {
 61         return senderMail;
 62     }
 63 
 64     public void setSenderMail(String senderMail) {
 65         this.senderMail = senderMail;
 66     }
 67 
 68     public String getReceiverName() {
 69         return receiverName;
 70     }
 71 
 72     public void setReceiverName(String receiverName) {
 73         this.receiverName = receiverName;
 74     }
 75 
 76     public String getReceiverMail() {
 77         return receiverMail;
 78     }
 79 
 80     public void setReceiverMail(String receiverMail) {
 81         this.receiverMail = receiverMail;
 82     }
 83 
 84     public String getContent() {
 85         return content;
 86     }
 87 
 88     public void setContent(String content) {
 89         this.content = content;
 90     }
 91 
 92     public String getDate() {
 93         return date;
 94     }
 95 
 96     public void setDate(String date) {
 97         this.date = date;
 98     }
 99 
100     public String getSenderWebSite() {
101         return senderWebSite;
102     }
103 
104     public void setSenderWebSite(String senderWebSite) {
105         this.senderWebSite = senderWebSite;
106     }
107 
108 }

/Apache-Velocity-java/vms/helloWorld.vm

1 ##test hello world!
2 
3 $hello

/Apache-Velocity-java/vms/userInfo.vm

 1 ##测试User
 2 
 3 A: what's your name?
 4 B: $user.name
 5 
 6 A: what's your birthday?
 7 B: $user.birthday
 8 
 9 A: what's your email address?
10 B: $user.email
11 
12 A: good!

/Apache-Velocity-java/vms/emailTemplate.vm

 1 ##测试 email
 2 
 3 $mail.senderName message notification
 4 Sender       :   $mail.senderName<$mail.senderMail>        
 5 Date         :   $mail.date
 6 Receiver     :   $mail.receiverName<$mail.receiverMail>
 7 
 8 Dear $mail.receiverMail:
 9 $mail.senderName send a message notification as following:
10 
11 $mail.content
12 
13 please quick login the $mail.senderWebSite message center and have a look.
14 
15                                                       $mail.senderName Team
16            

因为velocity源码中默认的编码为:

1 # ----------------------------------------------------------------------------
2 # T E M P L A T E  E N C O D I N G
3 # ----------------------------------------------------------------------------
4 
5 input.encoding=ISO-8859-1
6 output.encoding=ISO-8859-1

所以,如果出现乱码我们可以设置velocity的编码格式:

1     VelocityEngine velocityEngine = new VelocityEngine();
2     velocityEngine.setProperty("input.encoding", "UTF-8");
3     velocityEngine.setProperty("output.encoding", "UTF-8");
4     velocityEngine.init();

这样就可以解决velocity的乱码问题啦...

源码下载:http://files.cnblogs.com/hongten/Apache-Velocity-java.rar

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Java与Android技术栈

使用 Kotlin + Spring Boot 进行后端开发Kotlin示例一:结合 Redis 进行数据存储和查询示例二:结合 RxJava 模拟顺序、并发地执行任务总结

Kotlin 是一个基于 JVM 的编程语言,它的简洁、便利早已不言而喻。Kotlin 能够胜任 Java 做的所有事。目前,我们公司 C 端 的 Androi...

17630
来自专栏向治洪

史上最强Spring mvc入门

一、SpringMVC基础入门,创建一个HelloWorld程序   1.首先,导入SpringMVC需要的jar包。 ?   2.添加Web.xml配置...

364100
来自专栏公众号_薛勤的博客

2小时学会Spring Boot(IDE:eclipse)

1.)使启动类继承SpringBootServletInitializer 覆写configure()方法。

48040
来自专栏石奈子的Java之路

原 荐 SpringBoot 2.0 系列0

57730
来自专栏技术专栏

logback日志写入kafka遇到的那些坑

这两天在学习storm实时流的时候需要将logback日志写入kafka,这期间遇到了很多坑,这里把遇到的坑和解决的问题记录一下,和大家共勉

2.1K30
来自专栏技术记录

rabbitMQ教程(三) spring整合rabbitMQ代码实例

一、开启rabbitMQ服务,导入MQ jar包和gson jar包(MQ默认的是jackson,但是效率不如Gson,所以我们用gson) ? ?  二、发...

35880
来自专栏闻道于事

Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

48300
来自专栏一个会写诗的程序员的博客

Springboot使用JPA操作数据库第七章 使用JPA操作数据库

本章主要介绍如何在Spring Boot的Web应用中使用Mysq数据库,也充分展示Spring Boot的优势(尽可能少的代码和配置).

18530
来自专栏Android源码框架分析

获取Android设备DeviceId与反Xposed Hook技术

APP开发中常需要获取设备的DeviceId,以应对刷单,目前常用的几个设备识别码主要有IMEI(国际移动设备身份码 International Mobile ...

48320
来自专栏拭心的安卓进阶之路

Android 进阶9:进程通信之 AIDL 解析

在 Android 进阶7:进程通信之 AIDL 的使用 中我们使用 AIDL 实现了跨进程的通信,但是不清楚 AIDL 帮我们做了什么。 AIDL 的本质是...

241100

扫码关注云+社区

领取腾讯云代金券