FreeMarker_模板引擎_代码自动生成器_源码下载
首先我們先來認識一下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 }源碼下載:http://files.cnblogs.com/hongten/freemarker_file.zip
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的FreeMarker_模板引擎_代码自动生成器_源码下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TP类库解析和使用系列[Input类]
- 下一篇: 微软的SQLHelper类(含完整中文注