使用 IntraWeb (2) - Hello IntraWeb

來源:互聯網
上載者:User

標籤:c   class   a   http   tar   color   


IntraWeb 比我相像中的更貼近 VCL, 傳統的非可視組件在這裡大都可用(其內部很多複合屬性是 TStringList 類型的), 它的諸多可視控制項也是從 TControl 繼承下來的.
這或許是它曾被改名為 VCL For Web 的原因.

建立測試工程時, 一般要使用 StandAlone Application, 這方便調試; 具體發布時只需稍稍修改.
我建立了一個交 MyTest1 的工程, 自動產生的主要檔案有: MyTest1.dpr、ServerController.pas、UserSessionUnit.pas、Unit1.pas

MyTest1.dpr
program MyTest1;uses  IWRtlFix,  Forms,  IWStart,  UTF8ContentParser,  Unit1 in ‘Unit1.pas‘ {IWForm2: TIWAppForm},  ServerController in ‘ServerController.pas‘ {IWServerController: TIWServerControllerBase},  UserSessionUnit in ‘UserSessionUnit.pas‘ {IWUserSession: TIWUserSessionBase};{$R *.res}begin  TIWStart.Execute(True);end.{注釋}IWRtlFix          //它沒有公開任何功能Forms             //還是以前的那個 Forms, 現在叫 Vcl.Forms 了 IWStart           //一個 StandAlone 應用是它啟動的, 主要提供 Execute 這個 Class 方法UTF8ContentParser //但願有了它, IW 不再有亂碼的問題Unit1             //ServerController  //這才是 IntraWeb 工程的核心單元, 每個工程都會在該單元自動建立一個(只有一個) Server Controller 對象, 這個對象統管所有(包括 Session)UserSessionUnit   //該單元主要維護一個 Session 資料對象, 並提供給 ServerController 使用//這裡的代碼是自動維護的; 只有在修改發布模式時, 才會來這裡做簡單的修改(譬如把 program MyTest1 改為 library MyTest1)

UserSessionUnit.pas
unit UserSessionUnit;interfaceuses  IWUserSessionBase, SysUtils, Classes;type  TIWUserSession = class(TIWUserSessionBase)  private  public  end;implementation{$R *.dfm}end.{注釋}//該單元目前只有一個空的 TIWUserSession, 但也已被 ServerController 單元 uses 並使用//不同的網站程式對使用者資訊的需求可能不一樣(譬如: 登陸資訊、購物車資訊等等), 我們可以在這裡定義需要的資料格式; 因為靈活性很大, 所以關於 Session 的方便操作也是 IW 在宣傳時首先要吹噓的//在它的表單上可以放置非可視控制項//TIWUserSessionBase 的父類是我們熟悉的 TDataModule, 所以我說這是 Session 相關的資料模組.

ServerController.pas
unit ServerController;interfaceuses  SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,  // For OnNewSession Event  UserSessionUnit, IWApplication, IWAppForm, IW.Browser.Browser;type  TIWServerController = class(TIWServerControllerBase)    procedure IWServerControllerBaseNewSession(ASession: TIWApplication); //TIWServerControllerBase 的 OnNewSession 事件; 在初始化階段, 該事件就執行了  private  public  end;{下面兩個函數類似 Printer(Printers.pas), Clipboard(Clipbrd.pas), 可以讓我非常方便地使用最常用的對象; 但要使用它們需 uses ServerController}function UserSession: TIWUserSession;             //function IWServerController: TIWServerController; //也常常會使用 gServerController、gSC 代替它implementation{$R *.dfm}uses  IWInit, IWGlobal; //IWInit   提供了 WebApplication (類型是 TIWApplication), 從名字上就能感受到它的重要性                    //LWGlobal 提供了 gServerController(可以簡寫為 gSC)function IWServerController: TIWServerController;begin  Result := TIWServerController(GServerController); //GServerController = IWGlobal.gServerController; 在這之前它已建立, 這裡只是返回一下end;function UserSession: TIWUserSession;begin  Result := TIWUserSession(WebApplication.Data); //TIWApplication.Data 專門用於儲存 Session 資料end;{OnNewSession}procedure TIWServerController.IWServerControllerBaseNewSession(ASession: TIWApplication);begin  ASession.Data := TIWUserSession.Create(nil, ASession); //從其參數的命名上可以看出 Session 在 TIWApplication 心中的地位; 但 TIWApplication 還有更多功能end;initializationTIWServerController.SetServerControllerClass; //這是 TIWServerControllerBase 提供的 Class 方法; 它應該是做了一些必要的初始化工作, 但沒有源碼看不到.end.//在它的表單上可以放置非可視控制項

Hello IntraWeb先在 Unit1 的表單上放三個控制項:
IWLabel1;
IWButton1, 並啟用其 OnClick 事件;
IWButton2, 並啟用其 OnAsyncClick 事件.
{代碼}procedure TIWForm2.IWButton1Click(Sender: TObject);begin  IWLabel1.Caption := ‘Hello IntraWeb - OnClick‘; //這是全頁面重新整理的, 會看到 Loading 表徵圖end;procedure TIWForm2.IWButton2AsyncClick(Sender: TObject; EventParams: TStringList); //通過 AJAX 技術進行局部重新整理begin  IWLabel1.Caption := ‘Hello IntraWeb - OnAsyncClick‘; //這是局部重新整理的end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.