Android freemarker模板引擎应用

移动开发 Android
什么是freemarker?简单点就是,事先把上面这个html文件,放到应用中,用的时候只要传入数据就行。

什么是freemarker?

在说这个之前我们都知道web和原生控件之争就那么点事。性能,加载速度,流量,数据交互….

如果我用webView加载一个url页面,要先通过网络解析css,解析html代码,然后渲染生成页面

什么是freemarker?简单点就是,事先把上面这个html文件,放到应用中,用的时候只要传入数据就行

freemarker优点和应用

节约流量,加快网页加载速度

比如某些图表功能,用js库实现比较方便,只要事先放入html模板,传入数据就行。大大节省了流量及加载速度

或者事先已经有网页功能的页面,就不需要在制作Android界面了

此功能在IOS上通用,所以只要一个模板,就可以用在IOS和Android上,大大节约开发时间

实现原理

webView加载本地模板引擎流程

main.tpl ——–> main.ftl+数据 ———> main.html ———> webView.load(main.html)

1、导入freemarker库

  1. compile 'org.freemarker:freemarker-gae:2.3.25-incubating' 

2、将main.tpl文件放入assets目录下

  1. <!--main.tpl文件--> 
  2. <html> 
  3. <head> 
  4.   <title>Welcome!</title> 
  5. </head> 
  6. <body> 
  7.   <h1>Welcome ${user}!</h1> 
  8.   <p>Our latest product: 
  9. </body> 
  10. </html>  

3、根据main.tpl转成main.ftl

  1. private void prepareTemplate() throws IOException { 
  2.     //获取app目录  data/data/package/file/ 
  3.     String destPath = getFilesDir().getAbsolutePath(); 
  4.     File dir = new File(destPath); 
  5.     //判断文件夹是否存在并创建 
  6.     if (!dir.exists()) { 
  7.         dir.mkdir(); 
  8.     } 
  9.     //需要生成的.ftl模板文件名及路径 
  10.     String tempFile = destPath + "/" + "main.ftl"
  11.     if (!(new File(tempFile).exists())) { 
  12.         //获取assets中.tpl模板文件 
  13.         InputStream is = getResources().getAssets().open("main.tpl"); 
  14.         //生成.ftl模板文件 
  15.         FileOutputStream fos = new FileOutputStream(tempFile); 
  16.         byte[] buffer = new byte[7168]; 
  17.         int count = 0; 
  18.         while ((count = is.read(buffer)) > 0) { 
  19.             fos.write(buffer, 0, count); 
  20.         } 
  21.         fos.flush(); 
  22.         fos.close(); 
  23.         is.close(); 
  24.     } 
  25.  

4、将 main.ftl和数据 生成main.html文件

  1. private void genHTML(Product object) { 
  2.     String destPath = getFilesDir().getAbsolutePath(); 
  3.     FileWriter out = null
  4.     //数据源 
  5.     Map root = new HashMap(); 
  6.     root.put("user""user");   //传入字符串 
  7.     //root.put("product", object.url());     //传入对象(会报错) 
  8.     try { 
  9.         Configuration cfg = new Configuration(new Version(2,3,0)); 
  10.         cfg.setDefaultEncoding("UTF-8");   
  11.         //设置报错提示 
  12.         cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); 
  13.         //设置报错提示 
  14.         cfg.setLogTemplateExceptions(true); 
  15.         out = new FileWriter(new File(destPath + "main.html")); 
  16.         //设置.ftl模板文件路径 
  17.         cfg.setDirectoryForTemplateLoading(new File(destPath)); 
  18.         //设置template加载的.ftl模板文件名称 
  19.         Template temp = cfg.getTemplate("main.ftl"); 
  20.         //讲数据源和模板生成.html文件 
  21.         temp.process(root, out); 
  22.         out.flush(); 
  23.     } catch (MalformedTemplateNameException e) { 
  24.  
  25.     } catch (IOException e) { 
  26.  
  27.     } catch (Exception e){ 
  28.  
  29.     }finally { 
  30.         try { 
  31.             if (out != null
  32.                 out.close(); 
  33.         } catch (IOException e) { 
  34.             e.printStackTrace(); 
  35.         } 
  36.     } 
  37.  

5、webView加载main.html

  1. webview.post(new Runnable() { 
  2.     @Override 
  3.     public void run() { 
  4.         String templateDirRoot = getFilesDir().getAbsolutePath(); 
  5.         String url = "file://" + templateDirRoot + "main.html"
  6.         webview.loadUrl(url); 
  7.     } 
  8. });  

问题注意点

1、为什么要先把mian.tpl转成main.ftl文件,而不直接把mian.ftl文件放到assets中,然后template直接加载main.ftl文件

因为assets中的文件无法直接读取,所以要先把文件放到data/data/package/….再操作

2、突然发现2016年版的freemarker无法传递对象。

比如在main.ftl文件中${model.name}就无法再继续转成main.html,提示如下错误

  1. Unresolved exception class when finding catch block: java.beans.IntrospectionException 

官方说可以,但个人测试了无数遍,就是无法编译对象传值

如下方式可以获取到name

  1. //activity.java 
  2. User user = new User(); 
  3. user.setName="张三" 
  4. Map map = HashMap(); 
  5. map.put("name"user.getName()); 
  6.  
  7. //main.tpl 
  8. <html> 
  9. <body> 
  10.   ${name
  11. <body> 
  12. <html>  

如下方式无法获取到name

  1. //activity.java 
  2. User user = new User(); 
  3. user.setName="张三" 
  4. Map map = HashMap(); 
  5. map.put("user"user); 
  6.  
  7. //main.tpl 
  8. <html> 
  9. <body> 
  10.   ${user.name
  11. <body> 
  12. <html>  

总结

***没发现webView页面加载快多少,可能数据量少。毕竟要对SD卡操作。流量确实省了,也少了java和html直接的数据交互代码。

责任编辑:庞桂玉 来源: Android开发中文站
相关推荐

2012-03-06 15:34:05

JavaFreeMarker

2017-03-13 11:11:20

AndroidAndroid Stu文件组

2021-01-11 13:46:26

Spring BootThymeleafJava

2012-02-29 13:39:18

AndroidGoogle

2023-05-14 17:16:22

分类树SpringBoot

2011-07-07 16:15:20

Smarty

2017-01-04 15:22:57

TrimPath模板引擎

2012-04-30 20:54:01

Android

2019-11-13 09:01:48

开源JavaScript模板引擎

2011-07-15 14:01:50

PHP模板引擎

2023-11-10 09:16:45

SpringBootThymeleaf

2011-03-02 13:41:34

Action BarDashboardAndroid用户界面

2017-07-06 14:32:27

静态化FreeMarkerjava

2020-10-19 11:49:32

NodeJavaScript

2014-05-16 11:09:38

Handlebars模板引擎

2009-10-09 10:52:43

ASP.NET模板引擎

2022-11-30 15:15:48

2012-04-13 09:45:53

JavaScriptjQuery

2014-04-30 12:04:43

Velocity.js模板引擎

2009-12-23 18:06:25

WPF模板
点赞
收藏

51CTO技术栈公众号