| webclass实现动态web编程之实践篇 (作者:苏红超 2001年01月10日 15:50) 说了这么多的有关webclass理论方面的东西,想必大家也一定想要亲自试一试究竟webclass是怎么样一回事情了,那我们就先来看一个小的例子,也慢慢消化一下我们上面讲解到的各种理论知识: 启动vb6集成环境之后,新建一个“iis应用程序”的项目。这样缺省的情况下就会创建一个名称是project1的工程以及一个叫做webclass1的缺省的webclass对象。webclass的开发环境有两部分组成:webclass设计器和代码窗口。其中的webclass设计器的地位就像往常的vb表单设计器一样,通过他可以添加新的html模版webitems,添加custom webitems对象,以及在webitems中添加各种事件处理过程和启动停止本地的web server。当这个项目运行的时候,会初始化webclass对象,会触发webclass_start事件,也就是说这个事件类似于vb中的sub main函数,他会首先被触发。 我们来先写一段最简单的webclass程序代码: option explicit option compare text private sub webclass_start() response.write "<html>" response.write "<body>" response.write "<center>这是我们的第一个webclass程序!</center>" response.write "<center>当前的系统事件是:" & now() & "</center>" response.write "</body>" response.write "</html>" end sub 好了,我们需要保存这个工程,运行的时候vb会提示我们建立一个新的web,是为了可以在vb中运行我们的webclass工程的时候调试的方便。之后,我们运行这个项目,vb就会自动调用internet explorer浏览器浏览我们的程序,并且你可以随时在我们的webclass程序中设置端点来跟踪各种变量的值,这在普通的asp程序中是无法想象的事情,但是使用webclass却是如此的方便和自由! webclass使用webitem向浏览器提供内容和事件。每一个webclass都会包含多个webitems。而webitem则有两种类型:html template webitem和custom webitem。通过html template webitem可以将html文件和webitem联系在一起,所以在添加webitem之前必须创建我们需要的html文件。值得注意的是:当你在添加html模版的时候,如果模版文件和我们的工程文件不在同一个目录中,则系统会将模版的一个副本拷贝到工程所在的目录中;如果模版文件和工程文件在同一个目录中,则系统会重新命名这个模版副本的名字,而我们在需要改动模版的时候,需要改动的是这个模版的副本,而不是它本身! webitem使用方法writetemplate()来对响应输出模版内容。下面我们再来看一个结合了html template webitem的程序: 在我们刚才的webclass1工程中添加一个html template webitem,系统会提示我们选择一个html文件,我们选择的html文件是test.htm,内容很简单: <html> <body> <br>这是模版test中的内容:hello,world! </body> </html> 添加之后,我们会在webclass设计器中的“html template webitem”下面找到名为“template1”的一个模版,为了方便,我们更改他的名字为“test”。 这样我们的代码变动如下: option explicit option compare text '模版test的response事件 private sub test_respond() response.write "<h1><br>模版test中的response事件响应!</h1>" '调用writetemplate()方法来处理我们的名叫test的html模版文件 test.writetemplate end sub private sub webclass_start() '写一个对用户的回答 response.write "<html>" response.write "<body><h1>" response.write "<center>这是我们的第一个webclass程序!</center>" response.write "<center>当前的系统事件是:" & now() & "</center>" response.write "</h1></body>" response.write "</html>" '将控制权交给名为test的html模版,会首先触发他的response事件 set nextitem = test end sub 我们运行这一段webclass程序,则会在浏览器中显示如下(图1): 接下来我们看看custom webitem,这种类型的webitem没有相关的html文件,顾名思义,他是用户自定义的webitem,他是不可以使用writetemplate()方法的。同样可以通过设置nextitem属性来触发custome webitem。我们就不再重复了。 探讨完了两种类型的webitem,我们来看一个综合利用这两种类型webitem的例子: option explicit option compare text private sub custome_respond() response.write "<h1><br>用户自定义webitem(custome)的response事件响应!</h1>" response.write "<br><h1>接下来处理用户的选择!</h1>" '处理用户做出的选择 select case trim(request.form("userchoice")) case "普通工人" set nextitem = workerpage case "销售人员" set nextitem = salespage case "其它" set nextitem = otherpage end select end sub private sub otherpage_respond() response.write "<h1><br>模版otherpage的response事件响应!</h1>" otherpage.writetemplate end sub private sub salespage_respond() response.write "<h1><br>模版salespage的response事件响应!</h1>" salespage.writetemplate end sub private sub workerpage_respond() response.write "<h1><br>模版workerpage的response事件响应!</h1>" workerpage.writetemplate end sub private sub webclass_start() 'webclass启动的时候生成我们需要的一个表单 with response .write "<html><body><h1>" .write "<center>处理表单</center><br>" '设置表单提交到我们的custome webitem(custome)中,会首先触发custome的response事件 .write "<form name=test1 method=post action=" & urlfor(custome) & ">" .write "选择你喜欢的职业:<select name=userchoice>" .write "<option value=普通工人>普通工人" .write "<option value=销售人员>销售人员" .write "<option value=其它>其它" .write "</select><input type=submit value=确定></form>" .write "</h1></body></html>" end with end sub 其中的三个html模版内容如下: html模版otherpage: <html> <body> <h1>你选择的是其他职业!</h1> </body> </html> html模版salespage: <html> <body background=""> <h1> <br>你选择的是销售人员职业!</h1> </body> </html> html模版worker: <html> <body> <h1>你选择的是普通工人!</h1> </body> </html> 运行这个项目之后首先会出现如下叶面,让用户选择喜欢的职业: 如果用户做出了自己的选择,并提交了这个表单,则显示如下图: 我们知道在asp中可以动态的控制各种html元素,从而实现统一个asp文件可以实现不同的html叶面,那么在webclass中我们如何实现呢?这就需要在webclass中很重要的标签处理事件了,也就是在html template webitem中的processtag事件,也就是说在你调用一个html template webitem中的writetemplate()方法的时候就会触发他的processtag事件,从而也就开始了对html模版的标签处理程序。这个事件有如下参数: private sub templateitem_processtag(byval tagname as string, tagcontents as string, sendtags as boolean) end sub 其中的参数tagname是在html模版中你自行定义的标签名称,在默认的情况下,这个标签的前缀是wc@(当然你也可以自行改为自己想要的名字),在html中的代码一般如下:<wc@test>mytest</wc@test>。 而参数tagcontents则是标签所包围的内容了,你可以通过对这个参数的赋值来实现标签间内容的动态改变。 参数sendtags表示是否将标签本身也传送到浏览器,默认的情况下是false,也就是不将标签本身传送到浏览器。 我们现在来做一个使用processtag事件的具体例子,还是沿用我们上面的例子,不过我们加上,当用户选择的是其他的职业的时候,在otherpage这个html模版中我们定义一个标签:wc@other。 html模版otherpage的代码修改如下: <html> <body> <h1>你选择的是其他职业!</h1> <wc@other>test</wc@other> </body> </html> 这样当我们选择了“其他”的选项之后,从我们上面运行过的代码可以看出在otherpage_respond()事件中,我们调用了writetemplate方法,上面的例子中由于在html模版otherpage中没有任何标签,所以不会触发他的processtag事件,但是现在的例子则会触发相应的processtag事件,我们在相应的processtag事件中的代码如下: private sub otherpage_processtag(byval tagname as string, tagcontents as string, sendtags as boolean) '如果是标签wc@other,则我们进行相应的动态替换 if tagname = "wc@other" then tagcontents = "标签里面的内容已经被我们动态替换啦!" end if end sub 运行我们的这个例子,你就会发现位于标签<wc@other>和</wc@other>之间的内容"test"的确被我们动态的替换掉了! 通过上面的讲解和实际例子的联系,相信大家对于webclass的编程思想和思路有了一定的认识和了解,当然我们这篇文章只是引导大家进入webclass世界,真正的webclass编程仍然需要大家的不断实践才可以掌握,但是仅仅通过我们的这些简单介绍,大家也一定应当可以了解到webclass相对于纯asp来讲是一个很大的飞跃,他使得我们可以向往常的编写普通应用程序那样来编写各种web程序,给我们带来了极大的方便!我们在webclass的世界中可以使用所有vb程序可以使用的东西,包括各种activex dll对象等等,可以享受到vb带来的所有优点! 在文章的最后,我们来看一下webclass实现的一些“秘密”,仍然是用上面的我们的例子,我们的工程保存为project1,webclass被命名为webclass1,则当你运行这个项目的时候,你会发现在浏览器中间的url中显示的是类似下面的东西:http://localhost/project1/webclass1.asp。 怎么仍然调用的是一个asp文件呢?先不要奇怪,我们来看看这个asp文件的内容究竟是什么吧: <% server.scripttimeout=600 response.buffer=true response.expires=0 if (vartype(application("~wc~webclassmanager")) = 0) then application.lock if (vartype(application("~wc~webclassmanager")) = 0) then set application("~wc~webclassmanager") = server.createobject("webclassruntime.webclassmanager") end if application.unlock end if application("~wc~webclassmanager").processnostatewebclass "project1.webclass1", _ server, _ application, _ session, _ request, _ response %> 看到了吧,其实这个文件是系统自动生成的用来调用我们的webclass项目的一个文件,通过这个文件的内容我们就可以很容易的理解了其实webclass在本质上仍然是一个activex dll组件(在我们编译这个webclass项目的时候,你会发现生成的名字是project1.dll)!当然这个文件的内容我们是不可以随便改动的,否则会出现错误,这个文件是由系统自动产生的,无需我们多干涉的! 通过我们的介绍,大家也一定对webclass有了一个初步的认识和了解了,剩下的事情当然就是需要自己多多通过实践加以练习了,这样才能将webclass技术运用自如! (以上所有程序均在vb6.0中文企业版,windows2000 server中文版,iis5.0下运行通过) <全文完> |