偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android freemarker模板引擎應用

移動開發(fā) Android
什么是freemarker?簡單點就是,事先把上面這個html文件,放到應用中,用的時候只要傳入數(shù)據(jù)就行。

什么是freemarker?

在說這個之前我們都知道web和原生控件之爭就那么點事。性能,加載速度,流量,數(shù)據(jù)交互….

如果我用webView加載一個url頁面,要先通過網絡解析css,解析html代碼,然后渲染生成頁面

什么是freemarker?簡單點就是,事先把上面這個html文件,放到應用中,用的時候只要傳入數(shù)據(jù)就行

freemarker優(yōu)點和應用

節(jié)約流量,加快網頁加載速度

比如某些圖表功能,用js庫實現(xiàn)比較方便,只要事先放入html模板,傳入數(shù)據(jù)就行。大大節(jié)省了流量及加載速度

或者事先已經有網頁功能的頁面,就不需要在制作Android界面了

此功能在IOS上通用,所以只要一個模板,就可以用在IOS和Android上,大大節(jié)約開發(fā)時間

實現(xiàn)原理

webView加載本地模板引擎流程

main.tpl ——–> main.ftl+數(shù)據(jù) ———> 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、根據(jù)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.     //判斷文件夾是否存在并創(chuàng)建 
  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和數(shù)據(jù) 生成main.html文件

  1. private void genHTML(Product object) { 
  2.     String destPath = getFilesDir().getAbsolutePath(); 
  3.     FileWriter out = null
  4.     //數(shù)據(jù)源 
  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.         //講數(shù)據(jù)源和模板生成.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、突然發(fā)現(xiàn)2016年版的freemarker無法傳遞對象。

比如在main.ftl文件中${model.name}就無法再繼續(xù)轉成main.html,提示如下錯誤

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

官方說可以,但個人測試了無數(shù)遍,就是無法編譯對象傳值

如下方式可以獲取到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>  

總結

***沒發(fā)現(xiàn)webView頁面加載快多少,可能數(shù)據(jù)量少。畢竟要對SD卡操作。流量確實省了,也少了java和html直接的數(shù)據(jù)交互代碼。

責任編輯:龐桂玉 來源: Android開發(fā)中文站
相關推薦

2012-03-06 15:34:05

JavaFreeMarker

2017-03-13 11:11:20

AndroidAndroid Stu文件組

2021-01-11 13:46:26

Spring BootThymeleafJava

2023-05-14 17:16:22

分類樹SpringBoot

2017-01-04 15:22:57

TrimPath模板引擎

2011-07-07 16:15:20

Smarty

2012-02-29 13:39:18

AndroidGoogle

2019-11-13 09:01:48

開源JavaScript模板引擎

2012-04-30 20:54:01

Android

2011-07-15 14:01:50

PHP模板引擎

2023-11-10 09:16:45

SpringBootThymeleaf

2017-07-06 14:32:27

靜態(tài)化FreeMarkerjava

2024-11-29 12:58:13

2020-10-19 11:49:32

NodeJavaScript

2014-05-16 11:09:38

Handlebars模板引擎

2011-03-02 13:41:34

Action BarDashboardAndroid用戶界面

2022-04-15 07:21:12

架構開源模板引擎

2009-12-23 18:06:25

WPF模板

2009-10-09 10:52:43

ASP.NET模板引擎

2012-04-13 09:45:53

JavaScriptjQuery
點贊
收藏

51CTO技術棧公眾號