zoukankan      html  css  js  c++  java
  • freemarker,在servlet中使用freemarker

    一,在项目中编写***.ftl文件

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>测试</title>
        </head>
        <body>
            ${date?string("yyyy")}
        </body>
    </html>

    二,在servlet中使用freemarker

    @WebServlet(name="test",urlPatterns="/test")
    public class servlet_test extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private Configuration cfg;
        @Override
        public void init() throws ServletException {
            //initialize operation
            cfg=new Configuration(Configuration.VERSION_2_3_25);
            cfg.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/ftl");
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setCharacterEncoding("utf-8");
            Map data=new HashMap();
            data.put("date", new Date());
            //得到模板
            try {
                //将模板和数据结合,并返回浏览器
                Template template=cfg.getTemplate("1test.ftl");
                template.process(data, response.getWriter());
            } catch (Exception e) {
                //e.printStackTrace();
                response.getWriter().print("暂时无数据");
            }
        }
    }
  • 相关阅读:
    第五章 数据的共享与保护
    实验6
    实验5
    实验4 类与对象2)
    实验三 类与对象
    实验2
    2018—3-21第二章程序例题(2)
    第二章思维导图
    2018—3-18C++第二章程序例题
    汇编实验九
  • 原文地址:https://www.cnblogs.com/m01qiuping/p/6425313.html
Copyright © 2011-2022 走看看