让Nutz支持最快的模板引擎Smarty4j

Smarty4j是一个开源的模板引擎。没错,它就是著名的php模板引擎之Java移植版。 
它特点就是将模板文件或者字符串编译成java类直接执行,所以效率比一般的模板解释的方式处理要快。它发展较晚,所以没有 velocity、FreeMarker 有名,人气也比 php 版本的模板引擎差好多。 
但是它很快! 
本着怀疑一切的态度,我自己进行了实测。在渲染1000遍一个简单的页面时(只引入几种简单的数据类型), Smarty4j 耗时 16 毫秒,velocity 耗时 63 毫秒,FreeMarker 则用了 109 毫秒。渲染5000遍时,Smarty4j 耗时 172 毫秒,velocity 耗时 328 毫秒,FreeMarker 则用了 390 毫秒。(以上测试均为多次测试后取的平均值) 
复杂页面耗时相差更大。所以把项目里的 velocity 或 FreeMarker 换成 Smarty4j 可以使你的应用提速不少。 

已经有网友将 Smarty4j 提供了插件集成到 struts2 中了。那么能不能把它集成到国产的小巧的 Nutz MVC 框架中呢? 
翻看了 Nutz 的用户手册后发现这是件非常容易的事情。 

下面我们就开始吧: 

1、首先要实现视图适配器 
非常简单: 

Java代码  收藏代码
  1. /** 
  2.  * Smarty4j 视图适配器 
  3.  * @author QinerG(QinerG@gmail.com) 
  4.  */  
  5. public class SmartyViewMaker implements ViewMaker {  
  6.   
  7.     public View make(Ioc ioc, String type, String value) {  
  8.         if("st".equalsIgnoreCase(type)){  
  9.             return new SmartyView(value);  
  10.         }  
  11.         return null;  
  12.     }  
  13.   
  14. }  



2、然后再实现具体的视图解析器 

Java代码  收藏代码
  1. /** 
  2.  * 使用 Smarty4j 模板生成页面 
  3.  * @author QinerG(QinerG@gmail.com) 
  4.  */  
  5. public class SmartyView extends AbstractPathView implements View {  
  6.   
  7.     private final String ext = ".html";  
  8.     private static Engine engine = new Engine();//加载模板引擎  
  9.       
  10.     public SmartyView(String dest) {  
  11.         super(dest);  
  12.         engine.setTemplatePath("");  
  13.         engine.setDebug(true);  
  14.     }  
  15.   
  16.     /* 渲染页面 
  17.      * @see org.nutz.mvc.View#render(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) 
  18.      */  
  19.     public void render(HttpServletRequest req, HttpServletResponse resp,  
  20.             Object obj) throws Throwable {  
  21.         if ("".equals(engine.getTemplatePath())) {  
  22.             String realPath = req.getSession().getServletContext().getRealPath("/");  
  23.             engine.setTemplatePath(realPath);  
  24.         }  
  25.           
  26.         String path = evalPath(req, obj);  
  27.   
  28.         // 空路径,采用默认规则  
  29.         if (Strings.isBlank(path)) {  
  30.             path = Mvcs.getRequestPath(req);  
  31.             path = "WEB-INF"  
  32.                     + (path.startsWith("/") ? "" : "/")  
  33.                     + Files.renameSuffix(path, ext);  
  34.         }  
  35.         // 绝对路径 : 以 '/' 开头的路径不增加 '/WEB-INF'  
  36.         else if (path.charAt(0) == '/') {  
  37.             if (!path.toLowerCase().endsWith(ext))  
  38.                 path += ext;  
  39.         }  
  40.         // 包名形式的路径  
  41.         else {  
  42.             path = "WEB-INF/" + path.replace('.', '/') + ext;  
  43.         }  
  44.           
  45.         Template template = engine.getTemplate(path);  
  46.           
  47.         Context ctx = new Context(); // 生成数据容器对象  
  48.         ctx.set("obj", obj);  
  49.         ctx.set("request", req);  
  50.         ctx.set("response", resp);  
  51.         ctx.set("session", req.getSession());  
  52.           
  53.         template.merge(ctx, resp.getWriter());  
  54.     }  
  55. }  



好了,完成! 

那么具体怎么用呢? 
首先在主模块上声明使用 Smarty4j 适配器 

Java代码  收藏代码
  1. @Views({SmartyViewMaker.class})   
  2. public class MainModule { }  



然后在action上声明模板路径即可,如: 

Java代码  收藏代码
  1. @At("/index")  
  2.     @Ok("st:st.index")  
  3.     public void index() {  


这个action的模板将对应 WEB-INF/st/index.html 文件。 

当然模板路径也可以放在 WEB-INF 外面,如: 
@Ok("st:abc.bbc") 或 @Ok("st:/abc/bbc") 
对应的模板路径为: 
abc/bbc.html 

原文地址:https://www.cnblogs.com/u0mo5/p/4170863.html