首先我们先来认识一下Freemarker
1.what is the FreeMarker?
你可以到freemarker的官网上去,那里有很详细的介绍:http://freemarker.org/
这里大概说一下:FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。
FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层
的实现技术,而且还可以用于生成XML,JSP或Java 等。
大家只要知道freemarker是一个模板引擎就可以啦...
2.freemarker能够为我们做什么?
我想知道了freemarker是模板引擎以后,我们最关心的是这个东东能够为我们做些什么?
看看下面的demo,你也许就明白了
项目结构:
运行代码:
1 public static void main(String[] args) throws Exception {
2 helloWorld(FTLS_PATH, HONGTEN_HELLO_WORLD_FTL);
3 }
运行效果:
运行代码:
1 public static void main(String[] args) throws Exception {
2 myJavaFile(FTLS_PATH,BEAN_URL,HONGTEN_MY_JAVA_FILE_FTL);
3 }
运行效果:
生成的User.java文件:
==================================================================
代码部分: 你可以了解一下velocity(也是一个模板引擎):利用Velocity自动生成自定义代码_java版_源码下载
==================================================================
/freemarker/src/com/b510/freemarker/Bean.java
1 package com.b510.freemarker;
2
3 /**
4 * bean类
5 *
6 * @author hongten(hongtenzone@foxmail.com)<br>
7 * @date 2013-4-5
8 */
9 public class Bean {
10
11 /** bean 名称 */
12 private String name;
13 /** bean 首字母小写名称 */
14 private String lowerName;
15 /** bean 路径 */
16 private String beanUrl;
17 /** dao 路径 */
18 private String beanDaoUrl;
19 /** dao 实现路径 */
20 private String beanDaoImplUrl;
21 /** service 路径 */
22 private String beanServiceUrl;
23 /** service 实现路径 */
24 private String beanServiceImplUrl;
25
26 public String getName() {
27 return name;
28 }
29
30 public void setName(String name) {
31 this.name = name;
32 }
33
34 public String getLowerName() {
35 return lowerName;
36 }
37
38 public void setLowerName(String lowerName) {
39 this.lowerName = lowerName;
40 }
41
42 public String getBeanUrl() {
43 return beanUrl;
44 }
45
46 public void setBeanUrl(String beanUrl) {
47 this.beanUrl = beanUrl;
48 }
49
50 public String getBeanDaoUrl() {
51 return beanDaoUrl;
52 }
53
54 public void setBeanDaoUrl(String beanDaoUrl) {
55 this.beanDaoUrl = beanDaoUrl;
56 }
57
58 public String getBeanDaoImplUrl() {
59 return beanDaoImplUrl;
60 }
61
62 public void setBeanDaoImplUrl(String beanDaoImplUrl) {
63 this.beanDaoImplUrl = beanDaoImplUrl;
64 }
65
66 public String getBeanServiceUrl() {
67 return beanServiceUrl;
68 }
69
70 public void setBeanServiceUrl(String beanServiceUrl) {
71 this.beanServiceUrl = beanServiceUrl;
72 }
73
74 public String getBeanServiceImplUrl() {
75 return beanServiceImplUrl;
76 }
77
78 public void setBeanServiceImplUrl(String beanServiceImplUrl) {
79 this.beanServiceImplUrl = beanServiceImplUrl;
80 }
81
82 }
/freemarker/src/com/b510/freemarker/Annotation.java
1 package com.b510.freemarker;
2
3 /**
4 * 注释
5 *
6 * @author hongten(hongtenzone@foxmail.com)<br>
7 * @date 2013-4-5
8 */
9 public class Annotation {
10
11 /**
12 * 作者名称
13 */
14 private String authorName;
15 /**
16 * 作者邮箱
17 */
18 private String authorMail;
19 /**
20 * 日期
21 */
22 private String date;
23 /**
24 * 版本
25 */
26 private String version;
27
28 public String getAuthorName() {
29 return authorName;
30 }
31
32 public void setAuthorName(String authorName) {
33 this.authorName = authorName;
34 }
35
36 public String getAuthorMail() {
37 return authorMail;
38 }
39
40 public void setAuthorMail(String authorMail) {
41 this.authorMail = authorMail;
42 }
43
44 public String getDate() {
45 return date;
46 }
47
48 public void setDate(String date) {
49 this.date = date;
50 }
51
52 public String getVersion() {
53 return version;
54 }
55
56 public void setVersion(String version) {
57 this.version = version;
58 }
59
60 }
/freemarker/src/com/b510/freemarker/MyFreeMarker.java
1 /**
2 *
3 */
4 package com.b510.freemarker;
5
6 import java.io.File;
7 import java.io.FileWriter;
8 import java.io.OutputStreamWriter;
9 import java.io.Writer;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import freemarker.template.Configuration;
16 import freemarker.template.Template;
17
18 /**
19 * freemarker测试
20 *
21 * @author hongten(hongtenzone@foxmail.com)<br>
22 * @date 2013-4-5
23 */
24 public class MyFreeMarker {
25
26 private static Configuration configuration;
27 private static Template template;
28 private static Writer writer;
29 /**
30 * 模板文件的存放路径,这里是存放在项目根目录下的ftls文件夹中
31 */
32 public static final String FTLS_PATH = "ftls";
33
34 public static final String MESSAGE = "message";
35 public static final String HELLO_WORLD = "Hello World!";
36 public static final String HONGTEN_HELLO_WORLD_FTL = "hongten-helloworld.ftl";
37 public static final String HONGTEN_MY_JAVA_FILE_FTL = "hongten-myJavaFile.ftl";
38
39 // bean
40 public static final String BEAN = "bean";
41 public static final String BEAN_URL = "com.b510.bean";
42
43 // annotation
44 public static final String ANNOTATION = "annotation";
45 public static final String ANNOTATION_AUTHOR_NAME = "hongten";
46 public static final String ANNOTATION_AUTHOR_MAIL = "hongtenzone@foxmail.com";
47 public static final String ANNOTATION_VERSION = "1.0";
48
49 // date formate
50 public static final String DATE_FROMATE = "yyyy-MM-dd";
51
52 public static void main(String[] args) throws Exception {
53 // helloWorld(FTLS_PATH, HONGTEN_HELLO_WORLD_FTL);
54 myJavaFile(FTLS_PATH, BEAN_URL, HONGTEN_MY_JAVA_FILE_FTL);
55 }
56
57 /**
58 * 利用模板在控制台打印helloworld信息
59 *
60 * @param path
61 * 模板存放的路径
62 * @param ftlFile
63 * 模板文件
64 * @throws Exception
65 */
66 public static void helloWorld(String path, String ftlFile) throws Exception {
67 // 创建Freemarker配置实例
68 configuration = new Configuration();
69 configuration.setDirectoryForTemplateLoading(new File(path));
70
71 // 创建数据模型
72 Map<String, String> root = new HashMap<String, String>();
73 root.put(MESSAGE, HELLO_WORLD);
74
75 // 加载模板文件
76 template = configuration.getTemplate(ftlFile);
77
78 // 显示生成的数据,这里打印在控制台
79 writer = new OutputStreamWriter(System.out);
80 template.process(root, writer);
81 writer.flush();
82 writer.close();
83 }
84
85 /**
86 * 利用freemarker生成自定义的javaBean
87 *
88 * @param path
89 * 模板路径
90 * @param packageUrl
91 * javaBean的url,即package名称
92 * @param ftlFile
93 * 使用的模板文件
94 * @throws Exception
95 */
96 public static void myJavaFile(String path, String packageUrl, String ftlFile) throws Exception {
97 // 创建Freemarker配置实例
98 configuration = new Configuration();
99 configuration.setDirectoryForTemplateLoading(new File(path));
100
101 // 创建数据模型
102 Map<String, Object> root = new HashMap<String, Object>();
103 Bean bean = new Bean();
104 bean.setName("User");
105 bean.setLowerName("user");
106 bean.setBeanUrl(packageUrl);
107 root.put(BEAN, bean);
108
109 Annotation annotation = new Annotation();
110 annotation.setAuthorMail(ANNOTATION_AUTHOR_MAIL);
111 annotation.setAuthorName(ANNOTATION_AUTHOR_NAME);
112 annotation.setVersion(ANNOTATION_VERSION);
113 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FROMATE);
114 annotation.setDate(simpleDateFormat.format(new Date()));
115 root.put(ANNOTATION, annotation);
116
117 // 加载模板文件
118 template = configuration.getTemplate(ftlFile);
119
120 String beanPath = System.getProperty("user.dir") + "/src/" + packageUrl.replace(".", "/") + "/";
121 File filePath = new File(beanPath);
122 if (!filePath.exists()) {
123 filePath.mkdirs();
124 }
125
126 String filePathOfBean = beanPath + "/User.java";
127 File file = new File(filePathOfBean);
128 if (!file.exists()) {
129 file.createNewFile();
130 }
131
132 // 显示生成的数据
133 writer = new FileWriter(file);
134 template.process(root, writer);
135 writer.flush();
136 writer.close();
137 }
138 }
/freemarker/ftls/hongten-helloworld.ftl
1 ${message}
/freemarker/ftls/hongten-myJavaFile.ftl
1 package ${bean.beanUrl};
2
3 import java.util.Date;
4
5 /**
6 * @author ${annotation.authorName}(${annotation.authorMail})<br>
7 * @date ${annotation.date}
8 *
9 * @version ${annotation.version}
10 */
11 public class ${bean.name} {
12
13 /**
14 * id号
15 */
16 private Integer id;
17 /**
18 * 姓名
19 */
20 private String name;
21 /**
22 * 性别
23 */
24 private String sex;
25 /**
26 * 生日
27 */
28 private Date birthday;
29
30 public Integer getId() {
31 return id;
32 }
33
34 public void setId(Integer id) {
35 this.id = id;
36 }
37
38 public String getName() {
39 return name;
40 }
41
42 public void setName(String name) {
43 this.name = name;
44 }
45
46 public String getSex() {
47 return sex;
48 }
49
50 public void setSex(String sex) {
51 this.sex = sex;
52 }
53
54 public Date getBirthday() {
55 return birthday;
56 }
57
58 public void setBirthday(Date birthday) {
59 this.birthday = birthday;
60 }
61
62 }