美文网首页
新手入门学习Spring Freemarker教程解析

新手入门学习Spring Freemarker教程解析

作者: 平凡的柚子 | 来源:发表于2021-02-19 22:25 被阅读0次

    初步学习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,用于请求设置变量,并处理模版的输出:

    package com.test.servlet; 
    import java.io.IOException; 
    import java.io.Writer; 
    import java.util.HashMap; 
    import java.util.Map; 
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import freemarker.template.Configuration; 
    import freemarker.template.Template; 
    import freemarker.template.TemplateException; 
    @SuppressWarnings("serial") 
    public class HelloFreeMarkerServlet 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 root = new HashMap(); 
        root.put("message", "hello world"); 
        root.put("name", "java小强"); 
        // 获取模板文件 
        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(); 
      } 
    } 
    

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

    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
      <servlet> 
        <servlet-name>hello</servlet-name> 
        <servlet-class> 
          com.test.servlet.HelloFreeMarkerServlet 
        </servlet-class> 
      </servlet> 
      <servlet-mapping> 
        <servlet-name>hello</servlet-name> 
        <url-pattern>/hello</url-pattern> 
      </servlet-mapping> 
      <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
      </welcome-file-list> 
    </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>
    

    最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:11604713672

    相关文章

      网友评论

          本文标题:新手入门学习Spring Freemarker教程解析

          本文链接:https://www.haomeiwen.com/subject/owuoaktx.html