FreeMarker之根據模型生成HTML代碼與FreeMarker根據模型生成Java代碼,本質上是同樣的,關於生成Java代碼能夠參考個人這篇文章:FreeMarker之根據模板生成Java代碼css
1、導入依賴html
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.test.freemarker</groupId> <artifactId>testFreemarker</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> </dependencies> <build> <finalName>testFreemarker</finalName> </build> </project>
2、編寫模板java
在src/main/java下新建templates包,並在該包下新建user.ftl,內容以下所示:web
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用戶信息</title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" /> </head> <body style="font-family:'Courier New'"> <h3 class="text-center">這是用戶${username}的信息頁!</h3> <div class="col-md-6 column"> <table class="table table-bordered"> <tr> <th>用戶名</th> <th>密碼</th> <th>年齡</th> <th>地址</th> </tr> <tr> <td>${username}</td> <td>${password}</td> <td>${age}</td> <td>${address}</td> </tr> </table> </div> </body> </html>
3、編寫Java代碼apache
在src/main/java新增testFreeMarker包,並在該包下建立該兩個Java文件bootstrap
User.javaapp
package testFreemarker; public class User { private String username; private String password; private Integer age; private String address; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, Integer age, String address) { super(); this.username = username; this.password = password; this.age = age; this.address = address; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
CreateHtmlByFreemarker.javawebapp
package testFreemarker; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import freemarker.core.ParseException; import freemarker.template.Configuration; import freemarker.template.MalformedTemplateNameException; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateNotFoundException; public class CreateHtmlByFreemarker { private static final String TEMPLATE_PATH = "src/main/java/templates"; private static final String CLASS_PATH = "src/main/webapp/product"; public static void main(String[] args) { // step1 建立freeMarker配置實例 Configuration configuration = new Configuration(); Writer out = null; try { // step2 獲取模版路徑 configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH)); // step3 建立數據模型 Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("username", "zhangsan"); dataMap.put("password", "123456"); dataMap.put("age", "18"); dataMap.put("address", "test"); // step4 加載模版文件 Template template = configuration.getTemplate("user.ftl"); // step5 生成數據 File docFile = new File(CLASS_PATH + "\\" + "user.html"); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile))); // step6 輸出文件 template.process(dataMap, out); System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^user.ftl 文件建立成功 !"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != out) { out.flush(); } } catch (Exception e2) { e2.printStackTrace(); } } } }
4、運行CreateHtmlByFreemarker.javamaven
通常運行不報錯並輸出user.ftl建立成功的輸出語句就表示成功,這時只需刷新下,就能夠在對應的目錄下看到經過模板生成的html文件。post