最簡單的 Url 映射是使用 TIWAppForm 的 class 方法: SetURL;
THandlers 是 IntraWeb XIV 新增的內容處理器, 它能完成的不僅僅是 Url 映射(轉發?).
THandlers 通過虛擬路徑、虛擬文件名, 可以轉到或處理任何文件.
這個過程中會用到一個 TContentBase 類型的參數, TContentForm、TContentRedirect 是它的子類; 有時會需要從 TContentBase 繼承出解決更多問題的子類.
THandlers 所在單元及繼承鏈:
IW.Content.Handlers.THandlers < TObject
主要成員:
{添加到內容處理器: aPath: 虛擬路徑; aDocument: 虛擬文件夾名; aHandler: 要添加的內容}
class function Add(const aPath: string; const aDocument: string; aHandler: TContentBase): TContentBase
class function Add(const aDocument: string; aHandler: TContentBase): TContentBase
{添加為首頁}
class function AddStartHandler(const aPath: string; const aDocument: string; aHandler: TContentBase): TContentBase
class function GetStartHandler(aSession: TIWApplication): TContentBase
{添加對指定擴展名的統一處理}
class function AddForExtension(const aExt: string; aHandler: TContentBase): TContentBase
class function FindMatch(aFullPath: string): TContentBase
class procedure RegisterGetStartHandlerProc(aProc: TGetStartHandlerProc = procedure(aSession: TIWApplication; var aHandler: TContentBase) of object)
TIWAppForm.SetURL 方法應該用在 initialization 部分, 如:
{假如有三個窗體, 這是 Unit1 的部分代碼:}
//...
procedure TIWForm1.IWButton1Click(Sender: TObject);
begin
WebApplication.GoToURL('bbb.aspx');
// WebApplication.GoToURL('abc/ccc.xxx');
end;
initialization
TIWForm1.SetAsMainForm;
TIWForm1.SetURL('', 'aaa.html'); //參數 1 是虛擬路徑, '' 或 '/' 表示根路徑; 參數 2 是虛擬文件名
end.
{這是 Unit2 的部分代碼: -------------------------------------------}
//...
initialization
TIWForm2.SetURL('/', 'bbb.php'); //名字是虛擬的, 不是真的 php 文件
end.
{這是 Unit3 的部分代碼: -------------------------------------------}
//...
initialization
TIWForm3.SetURL('/abc/', 'ccc.xxx');
end.
{通過如上設置, 上面三個窗體就對應了下面三個網址(假定測試地址是: 127.0.0.1:8888)}
http://127.0.0.1:8888/aaa.html
http://127.0.0.1:8888/bbb.php
http://127.0.0.1:8888/abc/ccc.xxx
THandlers 測試一:
{代碼主要寫在 IWServerController 的 OnConfig 事件中, 下面是部分代碼:}
uses
IWInit, IWGlobal, Unit1, Unit2, Unit3, {Unit1-3 分別對應三個窗體}
IW.Content.Handlers, IW.Content.Base, IW.Content.Form, IW.Content.Redirect; {THandlers、TContentBase、TContentForm、TContentRedirect 分別需要的單元}
{IWServerControllerBase.OnConfig 事件; 之前我是在 OnCreate 中測試的, 官方建議這些代碼應該在 OnConfig 事件中}
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
THandlers.Add('', 'aaa.htm', TContentForm.Create(TIWForm1));
THandlers.AddStartHandler('', 'bbb.htm', TContentForm.Create(TIWForm2)); //純屬實驗, 把 TIWForm2 設為首頁
THandlers.AddForExtension('.php', TContentForm.Create(TIWForm3)); //只要訪問 *.php 的頁面, 就轉到 TIWForm3
THandlers.Add('', 'ccc.htm', TContentRedirect.Create('xxx.html')); //假如在 wwwroot 下有 xxx.html, 那么訪問 ccc.htm 就可以轉到 xxx.html
end;
THandlers 測試二: 關於自定義的 TContentBase, 官方給出了這樣的例子:
{自定義 TContentXML 的單元: -----------------------------------------------------------}
unit MyXml;
interface
uses Classes, IW.Content.Base, HTTPApp, IWApplication, IW.HTTP.Request, IW.HTTP.Reply;
type
TContentXML = class(TContentBase)
protected
function Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): Boolean; override;
public
constructor Create; override;
end;
implementation
uses IW.Content.Handlers, IWMimeTypes;
constructor TContentXML.Create;
begin
inherited;
mFileMustExist := False;
end;
function TContentXML.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): Boolean;
begin
Result := True;
if Assigned(aReply) then
begin
aReply.ContentType := MIME_XML;
aReply.WriteString('<xml>My xml content here</xml>');
end;
end;
end.
{ServerController 單元的部分相關代碼: --------------------------------------------------}
uses
IWInit, IWGlobal, IW.Content.Handlers, MyXml;
{IWServerControllerBase.OnConfig 事件}
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
THandlers.Add('', 'XmlTest', TContentXML.Create);
end;
{Unit1 單元的部分相關代碼: -------------------------------------------------------------}
procedure TIWForm1.IWButton1Click(Sender: TObject);
begin
WebApplication.GoToURL('XmlTest');
end;
