国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > WEB開(kāi)發(fā) > 正文

使用FreeMarker生成靜態(tài)HTML

2024-04-27 15:13:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

2、使用模板生成HTML代碼

2.1 數(shù)據(jù)模型

public class User {PRivate String username;private String passWord;private Integer age;private String address;//省略setter和getter方法 }

2.2 FreeMarker模板

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>用戶(hù)信息</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">這是用戶(hù)${username}的信息頁(yè)!</h3>    <div class="col-md-6 column">        <table class="table table-bordered">            <tr>                <th>用戶(hù)名</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>

2.3 使用FreeMarker生成HTML代碼

/**     * 使用模板生成HTML代碼     */    public static void createHtmlFromModel() {        FileWriter out = null;        try {            // 通過(guò)FreeMarker的Confuguration讀取相應(yīng)的模板文件            Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);            // 設(shè)置模板路徑            configuration.setClassForTemplateLoading(CreateHtmlByFreemarker.class, "/static/ftl");            // 設(shè)置默認(rèn)字體            configuration.setDefaultEncoding("utf-8");            // 獲取模板            Template template = configuration.getTemplate("user.ftl");            //設(shè)置模型            User user = new User("tom", "hahahah", 28, "上海市");                        //設(shè)置輸出文件            File file = new File("e:/html/result.html");            if(!file.exists()) {                file.createNewFile();            }            //設(shè)置輸出流            out = new FileWriter(file);            //模板輸出靜態(tài)文件            template.process(user, out);        } catch (TemplateNotFoundException e) {            e.printStackTrace();        } catch (MalformedTemplateNameException e) {            e.printStackTrace();        } catch (ParseException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (TemplateException e) {            e.printStackTrace();        } finally {            if(null != out) {                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }

3、使用String作為FreeMarker模板,生成HTML代碼

3.1 數(shù)據(jù)模型使用2.1模型

3.2 模板使用2.2模板

3.3 使用FreeMarker生成HTML代碼

/**     * 把模板讀入到String中,然后根據(jù)String構(gòu)造FreeMarker模板     */    public static void createHtmlFromString() {        BufferedInputStream in = null;        FileWriter out = null;        try {            //模板文件            File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/user.html");            //構(gòu)造輸入流            in = new BufferedInputStream(new FileInputStream(file));            int len;            byte[] bytes = new byte[1024];            //模板內(nèi)容            StringBuilder content = new StringBuilder();            while((len = in.read(bytes)) != -1) {                content.append(new String(bytes, 0, len, "utf-8"));            }                        //構(gòu)造Configuration            Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);            //構(gòu)造StringTemplateLoader            StringTemplateLoader loader = new StringTemplateLoader();            //添加String模板            loader.putTemplate("test", content.toString());            //把StringTemplateLoader添加到Configuration中            configuration.setTemplateLoader(loader);                        //構(gòu)造Model            User user = new User("tom", "kkkkk", 29, "北京山東");            //獲取模板            Template template = configuration.getTemplate("test");            //構(gòu)造輸出路            out = new FileWriter("e:/html/result.html");            //生成HTML            template.process(user, out);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (TemplateException e) {            e.printStackTrace();        } finally {            if(null != in) {                try {                    in.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(null != out) {                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }            }

4、使用String模板,模板中使用List

4.1 數(shù)據(jù)模型

public class Classes {    private String classId; // 班級(jí)ID    private String className; // 班級(jí)名稱(chēng)    private List<User> users;    public String getClassId() {        return classId;    }    //省略settter和getter方法}

4.2 FreeMarker模板

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>班級(jí)信息</title><!-- 新 Bootstrap 核心 CSS 文件 --><link rel="stylesheet"    href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" /><style type="text/css">    .table-align{        margin-left: 25%;    }</style></head><body style="font-family:'Courier New'">    <h3 class="text-center">下面是班級(jí)ID【${classId}】,班級(jí)名稱(chēng)【${className}】的人員信息</h3>    <div class="col-md-6 column table-align">        <table class="table">            <tr>                <th>姓名</th>                <th>密碼</th>                <th>年齡</th>                <th>地址</th>            </tr>            <!-- FreeMarker使用List循環(huán) -->            <#list users as user>                <tr>                    <td>${user.username}</td>                    <td>${user.password}</td>                    <td>${user.age}</td>                    <td>${user.address}</td>                </tr>            </#list>        </table>    </div></body></html>

4.3 使用FreeMarker生成HTML代碼

/**     * 根據(jù)String模板生成HTML,模板中存在List循環(huán)     */    public static void createHtmlFromStringList() {        BufferedInputStream in = null;        FileWriter out = null;        try {            //模板文件            File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/class.html");            //構(gòu)造輸入流            in = new BufferedInputStream(new FileInputStream(file));            int len;            byte[] bytes = new byte[1024];            //模板內(nèi)容            StringBuilder content = new StringBuilder();            while((len = in.read(bytes)) != -1) {                content.append(new String(bytes, 0, len, "utf-8"));            }                        //構(gòu)造Configuration            Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);            //構(gòu)造StringTemplateLoader            StringTemplateLoader loader = new StringTemplateLoader();            //添加String模板            loader.putTemplate("test", content.toString());            //把StringTemplateLoader添加到Configuration中            configuration.setTemplateLoader(loader);                        //構(gòu)造Model            Classes classes = new Classes();            classes.setClassId("23");            classes.setClassName("實(shí)驗(yàn)一班");            List<User> users = new ArrayList<User>();            User user = new User("tom", "kkkkk", 29, "北京");            users.add(user);            User user2 = new User("Jim", "hhhh", 22, "上海");            users.add(user2);            User user3 = new User("Jerry", "aaaa", 25, "南京");            users.add(user3);            classes.setUsers(users);            //獲取模板            Template template = configuration.getTemplate("test");            //構(gòu)造輸出路            out = new FileWriter("e:/html/result.html");            //生成HTML            template.process(classes, out);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (TemplateException e) {            e.printStackTrace();        } finally {            if(null != in) {                try {                    in.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(null != out) {                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }            }


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 克什克腾旗| 磴口县| 黄大仙区| 永德县| 松阳县| 长宁县| 黄骅市| 施甸县| 蓝山县| 许昌县| 锦州市| 罗平县| 桦川县| 波密县| 乳山市| 汉中市| 游戏| 湖北省| 射洪县| 上蔡县| 耿马| 七台河市| 罗山县| 漳浦县| 柳河县| 濉溪县| 泗阳县| 临猗县| 清原| 石屏县| 新野县| 桦南县| 荃湾区| 肥城市| 闻喜县| 岢岚县| 清徐县| 浮山县| 文化| 措勤县| 江陵县|