zoukankan      html  css  js  c++  java
  • Freemarker取list集合中数据(将模板填充数据后写到客户端HTML)

    1.模板写法:

        <html>  
          <head>  
                <title>freemarker测试</title>  
            </head>  
            <body>              
                <#list mylist as item>
                    ${item.name!}----------
                    ${item.password!}-------
                    ${item.sex!}----------
                    ${item.photo!}-----------<br/>
                </#list> 
            </body>  
        </html>  

    2.java代码

    package servlet;
    
    import java.io.IOException;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import bean.TestFreemarkerBean;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    @SuppressWarnings("serial")
    @WebServlet("/hello")
    public class hello extends HttpServlet {
        // 负责管理FreeMarker模板的Configuration实例
        private Configuration cfg = null;
    
        public void init() throws ServletException {
            // 创建一个FreeMarker实例
            cfg = new Configuration();
            // 指定FreeMarker模板文件的位置
            cfg.setServletContextForTemplateLoading(getServletContext(), "/WEB-INF/templates");
        }
    
        @SuppressWarnings("unchecked")
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 建立数据模型
            Map<String,Object> root = new HashMap();
            /*root.put("message", "第一个入门程序");
            root.put("name", "freemarker");*/
            List<TestFreemarkerBean> list = new ArrayList<>();
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
            root.put("mylist", list);
            // 获取模板文件
            Template t = cfg.getTemplate("test.ftl");
    
            // 使用模板文件的Charset作为本页面的charset
            // 使用text/html MIME-type
            response.setContentType("text/html; charset=" + t.getEncoding());
            Writer out = response.getWriter();
            // 合并数据模型和模板,并将结果输出到out中
            try {
                // t.process(list, out); // 往模板里写数据
    //            request.setAttribute("mylist", list);
                t.process(root, out); // 往模板里写数据
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    
        public void destroy() {
            super.destroy();
        }
    }

     3.测试结果:

  • 相关阅读:
    ACM的算法分类 2015-04-16 14:25 22人阅读 评论(0) 收藏
    初学Larevel 2014-08-21 11:24 90人阅读 评论(0) 收藏
    初学PHP&MySQL 2014-05-31 12:40 92人阅读 评论(0) 收藏
    codeforces 570 E. Pig and Palindromes (dp)
    codeforces 570 D. Tree Requests (dfs序)
    poj 2157 Maze (bfs)
    cf 570 C. Replacement (暴力)
    cf 570B B. Simple Game(构造)
    cf 570 A. Elections
    hdu 1429胜利大逃亡(续) (bfs+状态压缩)
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7237480.html
Copyright © 2011-2022 走看看