zoukankan      html  css  js  c++  java
  • freemarker将文件读写到HTML中

    freemarker将文件读写到HTML中


    1、设计思路

    (1)写freemarker模板方法

    (2)写测试文件方法

    (3)新建ftl文件

    (4)在指定的路径下,新建文件夹


    2、写freemarker模板方法

    /**
           * 输出文件到指定的路径下
           * @Title:printFile
           * @Description:
           * @param:@param name
           * @param:@param root
           * @param:@param outputFile
           * @return: void
           * @throws
           */
          public void printFile(String name,Map<String,Object> root,String outputFile)
          {
        	  FileWriter out = null;
        	  try 
        	  {
        		  //写入到指定的文件路径
        		  out = new FileWriter(new File("D:\MyEclipse\Maven\ftl\" + outputFile));
        		  Template temp = this.getTemplate(name);
        		  try 
        		  {
        			  temp.process(root, out);
    		  } 
        		  catch (TemplateException e) 
        		  {
        			  e.printStackTrace();
    		  }
    	  } 
        	  catch (IOException e) 
        	  {
        		  e.printStackTrace();
    	  }
        	  finally
        	  {
        		  if(out != null)
    		  try 
        		   {
    			//关闭文件流
    			out.close();
    		    } 
        		    catch (IOException e) 
        		    {
    			e.printStackTrace();
    		    }
        	  }
          }

    3、写测试文件方法

    /**
    	 * 
    	 * @Title:testFreemarkerFile
    	 * @Description:
    	 * @param:
    	 * @return: void
    	 * @throws
    	 */
    	@Test
    	public void testFreemarkerFile()
    	{
    		//创建数据模型
    		Map<String,Object> root = new HashMap<String,Object>();
    		//为数据模型添加值
    		root.put("username", "张三");
    		root.put("age", "22");
    		root.put("sex", "男");
    		//将数据模型和模板中的数据输出到控制台
    		ft.printFile("user.ftl", root,"user.html");
    	}

    4、新建ftl文件

    姓名:${username}
    年龄:${age}
    性别:${sex}

    5、新建文件夹

    D:MyEclipseMavenftl


    6、生成结果

    (1)生成user.html



    (2)控制台生成的结果

    姓名:张三
    年龄:22
    性别:男




  • 相关阅读:
    lnmp vhost 虚拟目录配置
    vi 编辑器常用命令(转)
    centos7 nginx 加入开机启动
    centos7 编译安装mysql
    IE8以下支持css3 border-radius渲染方法
    html5 web 摇一摇切换歌曲
    L0、L1与L2范数
    c++多线程编程:常见面试题
    核函数以及SVM相关知识(重点)
    梯度下降法的三种形式BGD、SGD以及MBGD
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13315086.html
Copyright © 2011-2022 走看看