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.测试结果:

 

posted @   QiaoZhi  阅读(6694)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示