laszlo,一个大量应用于interent应用程序的xml平台,最近成了开源代码.这个平台结构与xul和xaml十分相似,都是在浏览器中完成设计.laszlo使用macromedia flash作为它的执行平台,以此获得卓越的兼容性.
laszlo应用程序由lzx写成.lzx是基于xml的程序语言,可以用任何一个文字编辑器书写.当laszlo服务器访问这些文件,它将这些lzx文件转化为比特码或二进位形式,然后这些编码被送到浏览器中macromedia flash插件.你的服务器不需要macromedia flash成分.macromedia flash可用和兼容于在大多数浏览器.
你可以在java.net中查阅"laszlo: an open source framework for rich internet applications"(作者:bill grosso )来获取关于这种平台的基本原理.本文中,我将会谈论语言的一些基本原则,这些原则在你使用lzx编写程序时将会派上用场.至于更深一步的学习,可以求助于laszlo站点的文档.
设立laszlo的“开发便笺”
使用 我关于laszlo的评注,或者访问laszlo站点,下载安装laszlo.也许你已经安装了tomcat.假如这样的话,laszlo的安装文档将会在laszlo安装时建议暂停tomcat.假如你没有安装tomcat,laszlo将会附带一份tomcat拷贝.安装后,你可根据上面所提到的评注,将laszlo设为tomcat的另一个web应用。对于经验丰富的tomcat使用者,这仅仅是大概设置一下web浏览器的根.
我们写一段lzx文件来开始你的学习过程.代码如下:
<canvas height="500" width="800" debug="true">
<debug x="450" y="0" height="300"/></canvas>
当lzx文件被提交给浏览器,你的浏览器版面将会与图1相同.我把它叫做你的“开发便笺”.这是应用程序开发所必需的.初始设置允许你写一个调试器声明,将它们在调试器窗口显示.这个设置还允许在调试器外有足够的空间以存储视觉控制器.

编写类
现在你有了一个可以使用的开发便笺,你可以在 laszlo写一个类来开始编码练习.一个类的基本特点是它的一些局部变量.下面的代码定义一个类和类中的一些变量.假如不是由于xml句法的原因,laszlo的类定义与java类定义非常相似.与java不同的是,laszlo类型系统与动态语言如javascript非常类似;也就是说,它非常的灵活宽松.
<canvas width="800" debug="true"><debug x="450" y="0" height="300"/><class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/></class></canvas>
laszlo中,每一次动作都在<canvas>标记符中发生.如此例所示,一个lzx类在<canvas>标记符中定义.这个编码将一个类定名为test,属性名a1,b1.
实例化一个类
上述的编码定义了一个类,下面的编码将会说明如何实例化所定义的类test:
<canvas width="800" debug="true"><debug x="450" y="0" height="300"/><class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/></class><!--instantiating a class--><test name="testinstance"></test></canvas>
仔细看着,类名test是如何实例化为下一个节点或<test>标记符.实例化对象被命名为testinstance,当需要时,它可以作为变量canvas.testinstance被使用.
laszlo中属性的特点
laszlo中,每当一个属性的建立和更改时,一个onchange事件将会产生.对于那些由事件控制的可视化编程来说相当的棒.这种机制将会节省编码工作量,更不必提其可读性和易理解性.
本文的主要目的是向你展示如何设定一个属性,如何激活事件和怎样编码以响应这些事件.我将会展示怎样写一个关于属性a1的onchange事件.
<canvas width="800" debug="true"><debug x="450" y="0" height="300"/><class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/></class><!--instantiating a class--><test name="testinstance">
<!--demonstrating an onchange for attribute event-->
<method event="ona1" name="ona1method">
debug.write("hey this works");
</method>
</test></canvas>
看到方法怎样定义ona1事件了吗?当a1的属性被改变时事件将被激活.这个编码还展示了定义一个方法的基本语法.注意,与java不同,方法属于对象或实例,在类中并没有此方法.method块在调试器中写了一行.如果调试器被激活,此行会在调试器窗口中显示出来.
写lzx时,注意所有lzx的书写和命名对大小写不敏感.然而,在xml中定义节点和标示符,xml大小写敏感,javascript同样如此.所以当你面对laszlo时一定要紧记这个差别.
refining the "developer pad" with alerts
在javascript中编程,调用alert()方法进行调试是很平常的. 所以我在laszlo中寻找可以信赖的alert()方法,不过没有找到.但是却意外地找到一个名为alert的控制器(更确切地说,是一个模态对话框).我猜测我可以利用其来模拟alert功能,当作调试器使用.以下编码是我的尝试.此外,到目前为止,这些编码都是学习lzx的基本特征.
<canvas width="800" debug="true"><debug x="450" y="0" height="300"/><class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/></class><!--instantiating a class--><test name="testinstance">
<!--demonstrating an onchange for attribute event-->
<method event="ona1" name="ona1method">
debug.write("hey this works");
</method></test><!-- simulating an alert --><!-- instantiate a modal alert dialog --><alert name="myalert" width="100" y="100">
hi - initial text</alert><!-- a method to work the modal dialog --><method name="showalert" args="displaytext">
canvas.myalert.setattribute("text",displaytext);
canvas.myalert.open();</method><!-- testing the alert function --><button onclick="canvas.showalert('alert button pressed')">
show alert</button></canvas>
在上面的编码中,我创造了一个canvas的子类alert组件的实例,声明了alert的大小,还命名为myalert,并赋与其内容:"hi - initial text.".
我还写了一个showalert方法来显示对话框.此方法有一个参数叫做text.方法主体访问模式对话框,将其属性text传递至displaytext.然后,此方法在模式对话框上调用open方法.
我还制作了一个按钮叫做showalert().当showalert按钮被按下,你将会看到一个关于你文本的警告.由于这个按钮是canvas的子类,没有位置设置,它将在屏幕的左上角显示.可以使用下面的编码改变按钮位置,使它出现调试窗口的下面:
<button x="500" y="310"
onclick="canvas.respondtomethod()">show alert</button>
图2中观察结果.

探索属性及事件及联系
范例代码现在开始进一步探索属性与事件之间的联系.被用来测试alert的按钮也可以用来设定此前定义的属性.我想要展示的是设定这些属性如何触发相应的onchange方法.
以下编码是一个方法,respondtobutton(), 将在showalert按钮按下时被调用.
<canvas width="800" debug="true"><debug x="450" y="0" height="300"/><class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/></class><!--instantiating a class--><test name="testinstance">
<!--demonstrating an onchange for attribute event-->
<method event="ona1" name="ona1method">
debug.write("hey this works");
</method>
</test><!-- simulating an alert --><!-- a modal alert dialog --><alert name="myalert" width="100" y="100">
hi - initial text</alert><!-- a method to work the modal dialog --><method name="showalert" args="displaytext">
canvas.myalert.setattribute("text",displaytext);
canvas.myalert.open();</method><!-- testing the alert function --><button onclick="canvas.respondtobutton()">show alert</button><method name="respondtobutton" >
//prepare a string
var somestring =
"hey, i am setting the a1 attributes value";
//write a debug to see it
debug.write(somestring);
//call the alert to see it
showalert(somestring);
//go ahead and set the attribute
canvas.testinstance.setattribute("a1",'satya');
//the above will fire an event causing
//"ona1method" to execute.
//you will see this in the debugger</method><
闽公网安备 35060202000074号