Another JSP: Javascript Page
基于 XML 及 javascript技术,我准备设计一个全新的 模板语言,以带来一些全新的体验:
1、足够简单。如果还需要编写一个复杂的词法分析、模板语言的话,至少会花去我1周以上的时间。我希望这个模板语言能够在1-2天内就开发完成,而且足够强大。
2、容易学习。应该只有近可能少的规则,很容易学习。
3、良好的可视化编辑能力,因为,主要希望提供 HTML + WEB 的开发,而传统的JSP实际上已经很难进行Visual编辑和预览了。新的模板语言应尽可能支持预览能力。
我想我已经找到了一个满意的答案,我会在尽可能短的时间内(呵呵自能在Overtime的时间内了)
基本的规则:
1、源文件必须是XML,因此,在WEB上,只能支持XHTML。(原因:我不想编写烦人的词法分析)
2、脚本语言选择JavaScript。不定义自己的脚本语言。
| 在任何属性中,可以使用${expr},替换成js表达式 翻译成: out.print(“”) |
${book.name} | 文本节点中的${expr}替换成js表达式 |
var strings = [ “hello”, “world” ]; | 这个例子直接相当于: var strings = [“hello”, “world”]; |
for(var it in strings){ $it } | 这个被翻译成为: for(var it in strings){ out.print(“” + it + “) } |
for(var it in strings){ $it } | 与 out.print(“ for(var it in strings){ out.print(“” + it + “) } out.print(“”) |
| for(var arg in args){ out.print(“ } |
或许还会扩展新的规则,目前没有 | |
基本思路:
1、parse XHTML in DOM
2、process each nodes
a) Text Node
i. With ${..} expression – replace with
ii. Without ${..} expression
b) xs:script element or element has xs:script=”true”
i. text node as script
ii. process child elements
c) element has xs:wrapper
其它:
1、 JavascriptPageServlet – process .jsp file, compile to javascript and then evaluation it.
2、 Only if the .jsp modified, should the servlet recompile the script, otherwise reuse the compilation unit
3、 Prepare the varibles such as “request, response, session, application etc.” and a funtion print(), println(). That can be called in the script.