FrameMaker从零到学习编码

Freemaker中文教程

http://download.csdn.net/detail/ch656409110/4494063

这是我的代码地址

http://download.csdn.net/detail/fulq1234/9699798

 

0,frameMaker需要导入包,如果用maven,pom.xml

<dependency>
     <groupId>org.freemarker</groupId>
     <artifactId>freemarker</artifactId>
     <version>2.3.25-incubating</version>
   </dependency>



1,main函数执行

simpleFTL.ftl

您的年龄是${age},<br/>

您的姓名是${name}

SimpleFTL.java

package com;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
importfreemarker.template.Configuration;
importfreemarker.template.Template;
public class SimpleFTL {
         publicstatic void main(String args[]) throws Exception{
                   //1,读取framemaker的配置文件,F:\\CODE\\test.my\\src\\main\\java\\template是template的绝对路径
                   Configuration cfg=new Configuration();
                   cfg.setDirectoryForTemplateLoading(newFile("src\\main\\java\\template"));//路径可以是绝对路径,也可以是相对路径
                   //2,读取模板
                   Template t1=cfg.getTemplate("simpleFTL.ftl");//在配置文件下面的路径,此文件的扩展名可以任意。
                   Map map=new HashMap();
                   map.put("age","1");
                   map.put("name","小明");
                  
                   //写到控制台
                   Writer write=new OutputStreamWriter(System.out);
                   t1.process(map,write);
                   write.flush();
                   write.close();
                  
         }
}
 



ftl1.html

<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>FrameMaker</title>
<styletype="text/css">
   label{
      color:red;
   }
</style>
</head>
<body>
<h1>${user}</h1>
Our lastest products:
<a>${lastestProduct.url}</a><br/>
<span>${lastestProduct.name}</span>
<br/>
<#--多级变量,取到结果-->
<div><label>manylevel.name.name.name</label>:<span>${manylevel.name.name.name}</span></div>
<#--if用法-->
<div><label>if:</label><span><#if user=="Big Joe">我是自己</#if></span></div>
<#--list遍历ArrayList类型的数据-->
<div><label>list:</label><span><#list l as temp>${temp},</#list></span></div>
<#--list遍历数组-->
<div><label>array</label><span><#listsarr as temp>${temp},</#list></span></div>
<#--数组加脚标-->
<div><label>array[0]</label><span>${sarr[0]}</span></div>
<#--遍历list-->
<div><label>list</label><span>
   <#list["a","f","b"]+["aa","rr","cc"]astemp>
   ${temp},
   </#list>
</span></div>
<#--加号连接两个哈希表,如果有重复的话,那么选择+号右侧的优先。注意<#assign只有头,没有结尾-->
<div><label>list</label><span>
   <#assignages={"Joe":21,"KK":33}+{"Joe":30,"BB":60}>
   Joe:${ages.Joe}
   BB:${ages.BB}
  
</span></div>
<#--加减乘除运算-->
<div><label>+-x%:</label><span>${1+2/3}</span></div>
<#--如果想获取计算的整数部分,就使用int-->
<div><label>int</label><span>${(1+2/3)?int}</span></div>
<#--使用比较-->
<#if user=="Big Joe">
<div>It is BigJoe</div>
<#else>
<div>It is not BigJoe</div>
</#if>
 
<#--switch case方法-->
<div><label>switch</label>
<span>
<#switch 6>
   <#case 0>
   I am 0
   <#break>
   <#case 1>
   I am 1
   <#break>
   <#default>
   I am default
</#switch>
</span></div>
 
</body>
</html>


 

FTL1Servlet.java

package com;
 
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
 
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
 
importfreemarker.template.Configuration;
importfreemarker.template.Template;
importfreemarker.template.TemplateException;
@WebServlet(name="ftl1",urlPatterns="/ftl1")
public class FTL1Servlet extendsHttpServlet {
         protectedvoid doGet(HttpServletRequest request,HttpServletResponse response) throwsServletException,IOException{
                   //加载配置文件
                   Configuration cfg=new Configuration();
                   cfg.setDirectoryForTemplateLoading(newFile("F:\\CODE\\test.my\\src\\main\\java\\template"));
                   String ftl="ftl1.html";
                   //获取模板
                   Template t1=cfg.getTemplate(ftl);
                   //加载数据
                   Map root=this.getRootMap();                  
                  
                   Write rwriter=new OutputStreamWriter(response.getOutputStream());
                   try{
                            t1.process(root,writer);
                   }catch (TemplateException e) {
                            //TODO Auto-generated catch block
                            e.printStackTrace();
                   }
                   writer.flush();
                   writer.close();
                  
                  
         }
         protectedvoid doPost(HttpServletRequest request,HttpServletResponse response) throwsServletException,IOException{
                   doGet(request,response);
         }
        
         privateMap getRootMap(){
                   Map root=new HashMap();
                   root.put("user","Big Joe");
                   Map latest=new HashMap();
                   root.put("lastestProduct",latest);
                   latest.put("url","products/greenmouse.html");
                   latest.put("name","green mouse");
                  
                   //加载底层数据
                   Map level1=new HashMap();
                   level1.put("name","Little people");
                  Map level2=new HashMap();
                   level2.put("name",level1);
                   Map level3=new HashMap();
                   level3.put("name",level2);
                   root.put("manylevel",level3);
                  
                   Set set=new TreeSet();
                   set.add(1);
                   set.add(2);
                   set.add(11);
                   set.add(5);
                   set.add(1);
                   set.add(1);
                   set.add(1);
                   System.out.println("set="+set);
                   root.put("set1",set);
                   //List
                   ArrayList list=new ArrayList();
                   list.add(5);
                   list.add(1);
                   list.add(4);
                   list.add(5);
                   list.add(5);
                   list.add(5);
                   System.out.println("list="+list);
                   root.put("l",list);
                  
                   //数组
                   String[] sarr=new String[]{"一","四","三"};
                   root.put("sarr",sarr);
                   return root;
                  
         }
}


注意了,标准的模板文件扩展名是ftl,但是也可以是html的扩展名。

FrameMaker可以输出到控制台System.out

也可以通过Servlet,用浏览器访问,返回个html页面。response.getOutputStream()

 

没有更多推荐了,返回首页