zoukankan      html  css  js  c++  java
  • Freemarker 入门示例

    初步学习freemarker ,先做一个简单的HelloWord程序!

    新建一个WEB工程,下载(我使用的是freemarker-2.3.20)freemarker并导入freemarker.jar,在WEB-INF下新建文件夹templates用于存放模版文件
    在templates下新建test.ftl,这是示例模版文件。内容就是HTML内容,里面带有一个标记符,用于将来进行变量替换,内容如下:

    <html>
      <head>
            <title>freemarker测试</title>
        </head>
        <body>
            <h1>${message},${name}</h1>
        </body>
    </html>
    

      新建一个Servlet,用于请求设置变量,并处理模版的输出:

     1 package com.test.servlet;
     2 import java.io.IOException;
     3 import java.io.Writer;
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 import freemarker.template.Configuration;
    11 import freemarker.template.Template;
    12 import freemarker.template.TemplateException;
    13 @SuppressWarnings("serial")
    14 public class HelloFreeMarkerServlet extends HttpServlet {
    15     // 负责管理FreeMarker模板的Configuration实例
    16     private Configuration cfg = null;
    17     public void init() throws ServletException {
    18         // 创建一个FreeMarker实例
    19         cfg = new Configuration();
    20         // 指定FreeMarker模板文件的位置
    21         cfg.setServletContextForTemplateLoading(getServletContext(),
    22                 "/WEB-INF/templates");
    23     }
    24     @SuppressWarnings("unchecked")
    25     public void doPost(HttpServletRequest request, HttpServletResponse response)
    26             throws ServletException, IOException {
    27         // 建立数据模型
    28         Map root = new HashMap();
    29         root.put("message", "hello world");
    30         root.put("name", "java小强");
    31         // 获取模板文件
    32         Template t = cfg.getTemplate("test.ftl");
    33         // 使用模板文件的Charset作为本页面的charset
    34         // 使用text/html MIME-type
    35         response.setContentType("text/html; charset=" + t.getEncoding());
    36         Writer out = response.getWriter();
    37         // 合并数据模型和模板,并将结果输出到out中
    38         try {
    39             t.process(root, out); // 往模板里写数据
    40         } catch (TemplateException e) {
    41             e.printStackTrace();
    42         }
    43     }
    44     public void doGet(HttpServletRequest request, HttpServletResponse response)
    45             throws ServletException, IOException {
    46         doPost(request, response);
    47     }
    48     public void destroy() {
    49         super.destroy();
    50     }
    51 }

    注意要在你的web.xml中配置该Servlet:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     6     <servlet>
     7         <servlet-name>hello</servlet-name>
     8         <servlet-class>
     9             com.test.servlet.HelloFreeMarkerServlet
    10         </servlet-class>
    11     </servlet>
    12     <servlet-mapping>
    13         <servlet-name>hello</servlet-name>
    14         <url-pattern>/hello</url-pattern>
    15     </servlet-mapping>
    16     <welcome-file-list>
    17         <welcome-file>index.jsp</welcome-file>
    18     </welcome-file-list>
    19 </web-app>

    为了方便测试,访问工程直接跳转到Servlet,对主页index.jsp做一个简单修改:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()
    +":"+request.getServerPort()+path+"/";
    %>
    <html>
      <body>
        <%
        String mypath = "hello";
        response.sendRedirect(basePath + mypath);
        %>
      </body>
    </html>

    部署工程到Tomcat,启动并访问http://localhost:8080/xx。

  • 相关阅读:
    天才AI少年范浩强坚信“弄斧要到班门”
    向Excel说再见,神级编辑器统一表格与Python
    疯狂脑机接口计划:马斯克的 “读心术”
    Jenkins 学习资料
    VMware: windows8 与 虚拟机ubuntu 14.04 共享文件夹
    [转载]一个老软件测试工程师的日志
    python 解析 配置文件
    借助github搭建自己的博客
    [转载]你需要知道的 16 个 Linux 服务器监控命令
    [转载]Linux服务器性能评估与优化
  • 原文地址:https://www.cnblogs.com/go-skill/p/5458095.html
Copyright © 2011-2022 走看看