模板技术的革命-fastm

模 板技术的革命-fastm

Revolutionary Template Tech -- fastm
1. Overview
There are a lot of template techs on the world, like JSP, Taglib, Freemarker, Velocity, XSL, Jivan, XMLC, Tapestry, Wicket, JDynamiTe, etc.

For more info, please visit.

http://java-source.net/open-source/template-engines



The reason why I write “yet another template -- fastm” is just that I can not find a good enough one for me to use.



The templates fall into 2 categories:

(1) Script with logic (if, else, for); like JSP, Taglib, Freemarker, Velocity, XSL, Tapestry, etc.

(2) Resource without logic; like Jivan, XMLC, Wicket, fastm, etc.

Here I like to review these templates group by group to list their and disadvantages that I am not satisfied.


At first, people write HTML in the Servlet code. The code is polluted by the long HTML.

So JSP is made.  Then people write code in the HTML.  The HTML is polluted by code.

So Taglib is made. People write Taglib as code, since Taglib’s XML format seems look better than code in the HTML.

To display the Taglib in HTML Editors like Dreamweaver, Taglib Display Plugin is made. They are complex jobs.

As we can see, the whole thing is becoming more and more complex.

[size=medium]3. Freemarker, Velocity
Freemarker, Velocty are scripts containing logic like if, else, for, set, etc, which may affect the HTML display effect in the HTML browser.

4. Tapestry, Wicket
Tapestry, Wicket uses HTML tag as its logic tag or page component tag.

Tapestry contains logic like if, else, for, etc, while Wicket contains none.

Both Tapestry and Wicket have their own view model like Label, Link, Table, etc.

You have to write code to put your data into these view models.

5. Jivan, XMLC
www.jivan.org

Jivan and XMLC directly manipulate the HTML DOM node. Pure HTML, no any pollution.

You have to write code to put your data into the DOM node.

6. XSL
XSL directly manipulates the HTML DOM node too.

XSL can be seen as a little complex template script containing logic like match, choose, otherwise, etc.

7. JDynamiTe
http://sourceforge.net/projects/jdynamite

JDynamiTe ports PHPBB template to Java.

Like PHPBB template, you have to write code to search the resource block in the template and then use it.

To solve that problem, I wrote fastm.

8. fastm
Now it is fastm’s turn. Thanks for your patience. :-)

fastm tries to avoid above disadvantages and leverage their advantages.

fastm template is like PHPBB or JDynamiTe template, which uses XML comment as block mark and not contains any logic at all.



The formula of fastm is very simple and straight-forward:

Template DOM + Object DOM = Result HTML



For example, we have a list of User objects, which has name, address such properties.

We can display the users by following template



<!-- BEGIN DYNAMIC: users -->

Name: {name}

Address: {address}

<!-- END DYNAMIC: users -->



As we can see, XML Comment not affects HTML display effect in the Browser or HTML Editor like Dreamweaver.



For a if-else logic, like

<% if(name == null ){ %>

        No User Name Defined

<%}else if(isSuperUser(name)){%>

        Super User: <%= name %>

<%} else{%>

        Common User : <%= name %>

<%}%>



Fastm template should be:



<!-- BEGIN DYNAMIC: no_name -->

        No User Name Defined

<!-- END DYNAMIC: no_name -->



<!-- BEGIN DYNAMIC: super_user -->

        Super User: {name}

<!-- END DYNAMIC: super_user -->



<!-- BEGIN DYNAMIC: common_user -->

        Common User: {name}

<!-- END DYNAMIC: common_user -->



The logic is to be moved back to java code as:



Map map = new HashMap();

map.put(“name”, name);

If(name == null){
    map.put(“no_name”, map);

} else if(isSuperUser(name)){

    map.put(“super_user”, map);

}else{

    map.put(“common_user”, map);

}



Right. fastm template not supports script logic.

You have to write your logic in java code, like Jivan, XMLC, Wicket, etc.



As we can see, fastm is very easy and simple to use. And fastm has the fastest speed and smallest size among all template techs. 



9. The Good of Driving the Logic back to Java Code
(1) It solves the HTML code pollution. Template can be correctly displayed in the Browsers or HTML Editors.

(2) The logic in the Java code is easy to debug.

(3) The logic in the Java code is easy to reuse.



Here is the rationale of fastm:

Business logic is logic. Presentation logic is logic too.

Presentation logic should be organized neatly as Business logic too.

10. Designer friendly
fastm is quite “Designer friendly”.

fastm HTML template can be displayed correctly in the Browers or HTML editors. What you see is what you get”.

Microsoft provides XML formats of office documents.

For example, Excel (2002 or above) file can be saved and edited as XML format.

fastm works quite well for that too.

Fastm Excel XML template can be displayed correctly in the Excel too. “What you see is what you get”.

So in such kinds of “visual XML UI editors”, fastm has more advantages than scripts like JSP, Taglib, XSL, freemarker, velocity, Tapestry.

Wicket has the same “Designer friendly” effect as fastm too.

The most “Designer friendly” templates are Jivan, XMLC. They are just pure HTML, XML.

11. DOM Granularity, Reusability
Compared with XML DOM node, fastm Template DOM node is more flexible, since it is customized by user self.

For example, we want to display a list of users in two kinds of layout: list or table.



(1) XML DOM way

Java 代码
  1. <ul>  
  2.   
  3. <li> name: <span> show name here</span></li>  
  4.   
  5. </ul>  
  6.   
  7.    
  8.   
  9. <table>  
  10.   
  11. <tr>  
  12.   
  13. <td> name: <span> show name here</span></td>  
  14.   
  15. </tr>  
  16.   
  17. </table>  
<ul>  <li> name: <span> show name here</span></li>  </ul>     <table>  <tr>  <td> name: <span> show name here</span></td>  </tr>  </table>  




As we can see, the 2 DOM level are different.

“List” has 3 levels; “Table” has 4 levels.



You may need to write 2 sets of “DOM manipulation” code for the 2 DOMs.

The DOM can not be shared. You modified the DOM, and then output it.

Next time, you need to operation on a fresh-new DOM.



(2) fastm way

Java 代码
  1. <ul>  
  2.   
  3. <!-- BEGIN DYNAMIC: user -->  
  4.   
  5. <li> name: {name}</li>  
  6.   
  7. <!-- END DYNAMIC: user -->  
  8.   
  9. </ul>  
  10.   
  11.    
  12.   
  13. <table>  
  14.   
  15. <!-- BEGIN DYNAMIC: user -->  
  16.   
  17. <tr>  
  18.   
  19. <td> name: {user}</td>  
  20.   
  21. </tr>  
  22.   
  23. <!-- END DYNAMIC: user -->  
  24.   
  25. </table>  
<ul>  <!-- BEGIN DYNAMIC: user -->  <li> name: {name}</li>  <!-- END DYNAMIC: user -->  </ul>     <table>  <!-- BEGIN DYNAMIC: user -->  <tr>  <td> name: {user}</td>  </tr>  <!-- END DYNAMIC: user -->  </table>  




As we can see, both the “List” and “Table” templates have 2 levels.

We just need one set of code to get same Object DOM for the 2 Template DOMs.

Since the data is organized in the Object DOM, not directly put into the Template DOM, so the Template DOM can be shared. 

fastm Template DOM is read only and thread safe, which can be used any times by any number of concurrent programs.

12. Resource
The old version 1.0 alpha of fastm is on https://sourceforge.net/projects/fastm

The new version 1.0 M of fastm is on https://fastm.dev.java.net/

Please check the “Documents and Files”.

https://fastm.dev.java.net/servlets/ProjectDocumentList

That includes source, sample, doc of fastm1.0M, and view adapter for SpringMVC, WebWork.

https://fastm.dev.java.net/servlets/ProjectDocumentList?folderID=1552&expandFolder=1552&folderID=0



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/csdnbig/archive/2005/12/28 /563885.aspx

转载于:https://www.cnblogs.com/newstar0101/archive/2010/03/29/1700017.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值