zoukankan      html  css  js  c++  java
  • Freemarker-2.3.22 Demo

    package No04_条件判断;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateExceptionHandler;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
    
            // 设置模板的home目录,默认从此目录获取模板(此设置只需在应用中设置一次) 
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
            cfg.setDirectoryForTemplateLoading(new File("D:/freemarker_template_home/"));
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            
            // 获取模板
            Template temp = cfg.getTemplate("No04_条件判断.ftl");
            
            // 创建、设置数据模型 
            Map<String, Object> dataMap = new HashMap<String, Object>();
            
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            dataMap.put("userList", list);
            
            Map<String, Object> userMap = new HashMap<String, Object>();
            userMap.put("username", "Nick Huang");
            userMap.put("age", 20);
            userMap.put("nickname", null);
            list.add(userMap);
            
            userMap = new HashMap<String, Object>();
            userMap.put("username", "Viki Yang");
            userMap.put("age", 19);
            userMap.put("nickname", "Viki");
            list.add(userMap);
            
            userMap = new HashMap<String, Object>();
            userMap.put("username", "Teng Yang");
            userMap.put("age", 18);
            userMap.put("nickname", "Teng");
            list.add(userMap);
            
            // 将模板和数据模型合并 
            String filename = temp.getName();
            filename = filename.substring(0, filename.lastIndexOf(".") + 1) + "html";
            FileOutputStream fos = new FileOutputStream(new File("D:/freemarker_output_home/" + filename));
            Writer out = new OutputStreamWriter(fos);
            
            temp.process(dataMap, out);
            out.flush();
        }
    
    }
    No04_条件判断.ftl
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>User list</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>user name</th>
                    <th>age</th>
                    <th>nick name</th>
                </tr>
            </thead>
            <tbody>
                <#list userList as being>
                    <tr>
                        <th>
                            <#if being.username == 'Nick Huang'>
                                Junior Programmer (${being.username})
                                <#else>
                                ${being.username}
                            </#if>
                        </th>
                        <th>
                            <#if 19 < being.age >
                                Old
                                <#else>
                                Young
                            </#if>
                        </th>
                        <th>
                            <#-- 如果为空,取默认值 -->
                            <#if being.nickname??>
                                ${being.nickname}
                                <#else>
                                no value
                            </#if>
                            <#-- 以下为简单写法 -->
                            <#-- ${being.nickname ! 'no value'} -->
                        </th>
                    </tr>
                </#list>
            </tbody>
        </table>
    </body>
    </html>
  • 相关阅读:
    关于iOS开发property with 'retain(or strong)' attribute must be of object type
    机器学习之神经网络
    一些知名的开源社区
    机器学习之正则化
    机器学习之逻辑回归(logistic回归)
    机器学习之正规方程法
    机器学习之线性回归、多项式回归
    机器学习之梯度下降法
    64位windows7下安装python,配置numpy和matplotlib库
    mysql分区查询
  • 原文地址:https://www.cnblogs.com/nick-huang/p/4611391.html
Copyright © 2011-2022 走看看