📄 ch03s02.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>2. Counter例子与回退键处理</title><link rel="stylesheet" href="html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.1"><link rel="start" href="index.html" title="Java网络程序员看Continuation"><link rel="up" href="ch03.html" title="Chapter 3. Seaside框架"><link rel="prev" href="ch03s01.html" title="1. 安装和启动Seaside"><link rel="next" href="ch03s03.html" title="3. Multi例子与对象模型"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2. Counter例子与回退键处理</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03s01.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Seaside框架</th><td width="20%" align="right"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e245"></a>2. Counter例子与回退键处理</h2></div></div></div><p>现在已经在运行的Seaside程序中最简单的莫过于counter了。单击counter链接,我们看到如下页面:</p><div class="screenshot"><div class="mediaobject"><img src="resources/counter.png"></div></div><p>您可以单击++和—符号看看counter程序的用途。很简单的程序对吧?现在我们试试用后退键,例如,增加两次,我们得到2。这时按后退键回到0,然后按++,您看到几?3还是1?</p><p>您会看到1。一般的网络程序会把counter的值储存在session中,这样的话会给出3,不过Seaside是基于continuation的,所以如果您按回退后再提交请求,那么服务器端会先跳回当时的状态再重新处理该请求。这样,0增加后一定是1,您永远不会看到0突然变成3的情况。另外,如果您在两个窗口同时使用counter程序,它们之间不会互相影响,这是基于cookie和session的传统程序无法做到的。</p><p>现在我们来看看counter程序的源代码。单击页面底部的Toggle Halos链接,您会看到下面的页面:</p><div class="screenshot"><div class="mediaobject"><img src="resources/counter-halos.png"></div></div><p>这说明0和两个链接是一个叫WACounter的部件生成的。我们单击扳手型图标就能看到该类的代码:</p><div class="screenshot"><div class="mediaobject"><img src="resources/counter-browser.png"></div></div><p>如果您曾经使用过Squeak,那么您马上就能认出这个页面是模仿Squeak里的system browser(系统浏览器)写的。不错,它的功能和用法都和system browser一模一样,也就是说,下面讲的操作可以在Squeak中同样完成。如果您真的在做开发,那么您绝对应该用Squeak里的system browser。不过为了简单起见,这里我们在浏览器中讲解和编写Seaside代码。</p><p>我们来看看WACounter这个类。如上图所示,其定义为:</p><pre class="programlisting">WAComponent subclass: #WACounter instanceVariableNames: 'count' classVariableNames: '' poolDictionaries: '' category: 'Seaside-Examples-Test'</pre><p>Smalltalk的语法对于习惯了C风格语言的程序员来说相当奇怪。我来简单解释一下,上面的定义是说,WACounter是WAComponent的子类,它有一个instance variable,叫做count。它的类别(Category)是Seaside-Examples-Test,注意smalltalk和Java不同,smalltalk没有Java里package的概念。这里的类别只是注释用的;smalltalk要求所有的类不能同名,不管它们是不是在同一个category中。</p><p>最右边一栏显示WACounter有5个method,分别是count, decrease, increase, initialize和renderContentOn。其中initialize会在对象创建时调用。单击最右边一栏的initialize,我们就会看到其代码:</p><pre class="programlisting">initialize self session registerObjectForBacktracking: self. count _ 0</pre><p>上面的代码写成Java风格如下:</p><pre class="programlisting">void initialize() { this.session().registerObjectForBacktracking(this); count = 0;}</pre><p>第一句通知session管理本对象,这样我们的WACounter对象就会被保存到continuation当中。Seaside可以只用continuation管理一部分而不是全部对象,这是因为有时候用户回退时,我们并不希望让程序完全倒退到当时的状态。例如在网络商店中,即使用户连按回退键10次,我们也不想让他已经放在“购物篮”里的商品突然都不见了。一般来说,有业务意义的对象(特别是涉及数据库的对象)不应该用continuation管理,而涉及用户界面的对象则应该用continuation管理。</p><p>第二句将count初始值设为0。我们可以看到要使用continuation事实上非常简单。另外,您也应该注意到基于continuation的框架不需要session的概念,例如WACounter中的count,会自动在continuation中保存起来,所以我们不需要另外把它放到一个特殊的session对象中去。</p><p>我们再来看看renderContentOn函数,其代码如下:</p><pre class="programlisting">renderContentOn: html html heading: count. html anchorWithAction: [self increase] text: '++'. html space. html anchorWithAction: [self decrease] text: '--'</pre><p>该函数负责生成HTML代码,它接受一个参数,html。整个函数简单地调用html的方法:heading,achorWithAction和space。Heading方法生成HTML的标题:<h1></h1>。同样的,achorWithAction生成超链接<a href=”…”></a>,space生成空格&nbsp;。比较特别的是anchorWithAction后面的方括号,self increase和self decrease(相当与this.increase()和this.decrease())。在Seaside中,我们把超链接所激活的操作直接放在anchorWithAction的参数里,而不用为每个超链接想一个特定的名字,之后再根据这个名字决定要进行的操作。Java的tapestry也试图完成同样的任务,不过smalltalk灵活的语法使得Seaside非常简洁流畅。</p><p>剩下的三个函数,count,increase和decrease都非常简单,我们就不在这里浪费笔墨了,有兴趣的读者可以自己研究一下。</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03s01.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1. 安装和启动Seaside </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3. Multi例子与对象模型</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -