• freemaker小练习


    public class TestFreemaker extends HttpServlet{
        // 负责管理FreeMarker模板的Configuration实例  
        private Configuration cfg = null;  
        public void init() throws ServletException {  
            // 创建一个FreeMarker实例  
            cfg = new Configuration();  
            // 指定FreeMarker模板文件的位置  
    //        cfg.setServletContextForTemplateLoading(getServletContext(),"/WEB-INF/templates");  
            cfg.setClassForTemplateLoading(TestFreemaker.class, "/templates");
        }  
        @SuppressWarnings("unchecked")  
        public void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            // 建立数据模型  
            Map root = new HashMap();  
            root.put("message", "hello world======");  
            root.put("name", "hhhhhh");  
            // 获取模板文件  
            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(root, out); // 往模板里写数据  
            } catch (TemplateException e) {  
                e.printStackTrace();  
            }  
        }  
        public void doGet(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            doPost(request, response);  
        }  
        public void destroy() {  
            super.destroy();  
        } 

  • 相关阅读:
    PHP防跨站之open_basedir目录设置
    在线Jupyter平台,验证手机号可免费使用
    wget命令之用户密码cookies断点续传指定类型整站下载
    linux控制台输出到文件
    结构体取变量
    常用编码解码识别
    #define和const
    做一个自己的清理临时文件的小程序(附成品&升级版)
    C盘清理/瘦身
    修复Word启动失败 报错WPS32
  • 原文地址:https://www.cnblogs.com/ly-china/p/5567701.html
Copyright © 2020-2023  润新知