Freemarker模板文件返回字符串

Freemarker模板文件返回字符串 

首先,先创建一个ftl文件: 
    Html代码

Html代码 

 收藏代码

  1. <div style="width:100%;font-size:12px;">Hello ${name}(${getUserAge(name)})</div>  

 之后,创建一个java应用程序类: 
   Java代码

Java代码 

 收藏代码

  1. package freemarker;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.StringWriter;  
  6. import java.io.Writer;  
  7. import java.util.HashMap;  
  8. import java.util.Locale;  
  9. import java.util.Map;  
  10.   
  11. import freemarker.template.Configuration;  
  12. import freemarker.template.DefaultObjectWrapper;  
  13. import freemarker.template.Template;  
  14. import freemarker.template.TemplateException;  
  15.   
  16. /** 
  17.  *  
  18.  * @Desc 获取FTL文件生成的字符串,以供调用 
  19.  * @author xujp1 
  20.  * @version Revision: 1.00 Date: May 9, 2012 
  21.  */  
  22. public class GenerateStringFromFtl  
  23. {  
  24.   
  25.     private static Configuration conf = null;  
  26.   
  27.     public static void main(String args[])  
  28.     {  
  29.         conf = new Configuration();  
  30.         try  
  31.         {  
  32.             conf.setDirectoryForTemplateLoading(new File("WebRoot/WEB-INF/template"));  
  33.         }  
  34.         catch (IOException e)  
  35.         {  
  36.             e.printStackTrace();  
  37.         }  
  38.         conf.setObjectWrapper(new DefaultObjectWrapper());  
  39.         conf.setLocale(Locale.CHINA);  
  40.         conf.setSharedVariable("getUserAge", new GetUserAge());//自定义方法供调用  
  41.         conf.setDefaultEncoding("utf-8");  
  42.         conf.setClassicCompatible(true);//处理空值为空字符串  
  43.         String tempReturn = "";  
  44.         Map<String, Object> root = new HashMap<String, Object>();  
  45.         String name = "xujp1";  
  46.         root.put("name", name);  
  47.         try  
  48.         {  
  49.             tempReturn = generateHtmlFromFtl(root, "hellouser.ftl");  
  50.         }  
  51.         catch (IOException e)  
  52.         {  
  53.             e.printStackTrace();  
  54.         }  
  55.         catch (TemplateException e)  
  56.         {  
  57.             e.printStackTrace();  
  58.         }  
  59.         System.out.println(tempReturn);  
  60.     }  
  61.   
  62.     public static String generateHtmlFromFtl(Object root, String tempPath) throws IOException, TemplateException  
  63.     {  
  64.         Template temp = conf.getTemplate(tempPath);  
  65.         Writer out = new StringWriter(2048);  
  66.         temp.process(root, out);  
  67.         return out.toString();  
  68.     }  
  69. }  

 由于有用到自定义方法,因此在建个方法类: 
   Java代码

Java代码 

 收藏代码

  1. package freemarker;  
  2.   
  3. import java.util.List;  
  4.   
  5. import freemarker.template.SimpleScalar;  
  6. import freemarker.template.TemplateMethodModel;  
  7. import freemarker.template.TemplateModelException;  
  8.   
  9. /** 
  10.  *  
  11.  * @Desc freemarker中使用的方法,根据传入的参数返回相应的值 
  12.  * @author xujp1 
  13.  * @version Revision: 1.00 Date: May 9, 2012 
  14.  */  
  15. public class GetUserAge implements TemplateMethodModel  
  16. {  
  17.   
  18.     /* (non-Javadoc) 
  19.      * @see freemarker.template.TemplateMethodModel#exec(java.util.List) 
  20.      */  
  21.     @SuppressWarnings("unchecked")  
  22.     @Override  
  23.     public Object exec(List args) throws TemplateModelException  
  24.     {  
  25.         if(args.size() != 1)  
  26.         {  
  27.             throw new TemplateModelException("Wrong arguments!");  
  28.         }  
  29.         int age = 0;  
  30.         if("xujp1".equalsIgnoreCase((String)args.get(0)))  
  31.             age = 25;  
  32.         else  
  33.             age = 24;  
  34.         return new SimpleScalar(String.valueOf(age));  
  35.     }  
  36. }  

 运行后,得到所要的结果

转自:https://zzc1684.iteye.com/blog/2101317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值