IT资讯

Android freemarker模板引擎应用

作者:admin 2021-09-15 我要评论

什么是freemarker? 在说这个之前我们都知道web和原生控件之争就那么点事。性能,加载速度,流量,数据交互. 如果我用webView加载一个url页面,要先通过网络解析c...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

什么是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直接的数据交互代码。


本文转载自网络,原文链接:http://mp.weixin.qq.com/s/Rg4U1ntEx6czKdk-SvVCSA

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • Android freemarker模板引擎应用

    Android freemarker模板引擎应用

  • 关于Android适配,看完这篇文章就够了

    关于Android适配,看完这篇文章就够了

  • RecyclerView实现滑动删除和拖拽功能

    RecyclerView实现滑动删除和拖拽功能

  • 主线程中也不绝对安全的UI操作

    主线程中也不绝对安全的UI操作

腾讯云代理商
海外云服务器