阳江网站推广优化公司,东丽手机网站建设,新手seo网站做什么类型好,玫瑰在线 网站建设内容首先我们先来认识一下Freemarker 1.what is the FreeMarker? 你可以到freemarker的官网上去#xff0c;那里有很详细的介绍#xff1a;http://freemarker.org/ 这里大概说一下#xff1a;FreeMarker是一个用Java语言编写的模板引擎#xff0c;它基于模板来生成文本输出。 …首先我们先来认识一下Freemarker 1.what is the FreeMarker? 你可以到freemarker的官网上去那里有很详细的介绍http://freemarker.org/ 这里大概说一下FreeMarker是一个用Java语言编写的模板引擎它基于模板来生成文本输出。 FreeMarker与Web容器无关即在Web运行时它并不知道Servlet或HTTP。它不仅可以用作表现层 的实现技术而且还可以用于生成XMLJSP或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(hongtenzonefoxmail.com)br7 * date 2013-4-58 */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(hongtenzonefoxmail.com)br7 * date 2013-4-58 */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(hongtenzonefoxmail.com)br22 * date 2013-4-523 */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 // bean40 public static final String BEAN bean;41 public static final String BEAN_URL com.b510.bean;42 43 // annotation44 public static final String ANNOTATION annotation;45 public static final String ANNOTATION_AUTHOR_NAME hongten;46 public static final String ANNOTATION_AUTHOR_MAIL hongtenzonefoxmail.com;47 public static final String ANNOTATION_VERSION 1.0;48 49 // date formate50 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 path61 * 模板存放的路径62 * param ftlFile63 * 模板文件64 * throws Exception65 */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 MapString, String root new HashMapString, 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生成自定义的javaBean87 * 88 * param path89 * 模板路径90 * param packageUrl91 * javaBean的url即package名称92 * param ftlFile93 * 使用的模板文件94 * throws Exception95 */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 MapString, Object root new HashMapString, 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})br7 * 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 } 源码下载http://files.cnblogs.com/hongten/freemarker_file.zip