惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 万一

关于内存数据与 JSON 高亮 TRichEdit 当前行 使用 IntraWeb (45) - 活用 IntraWeb 使用 IntraWeb (44) - 测试读取 SqLite (三) 使用 IntraWeb (43) - 测试读取 SqLite (二) 使用 IntraWeb (42) - 测试读取 SqLite (一) 使用 IntraWeb (41) - 数据控件速查 使用 IntraWeb (40) - 自定义 Session 数据 使用 IntraWeb (39) - THttpRequest、THttpReply 使用 IntraWeb (38) - TIWAppForm、TIWForm、TIWBaseHTMLForm、TIWBaseForm 使用 IntraWeb (37) - TIWApplication 使用 IntraWeb (36) - TIWServerControllerBase 使用 IntraWeb (35) - TIWJQueryWidget 使用 IntraWeb (34) - TIWAJAXNotifier 使用 IntraWeb (33) - Cookie 使用 IntraWeb (31) - IntraWeb 的 Xml 操作使用的是 NativeXml 使用 IntraWeb (30) - TIWAppInfo、TIWMimeTypes、TIWAppCache 使用 IntraWeb (29) - 基本控件之 TIWAutherList、TIWAutherINI、TIWAutherEvent 使用 IntraWeb (28) - 基本控件之 TIWTemplateProcessorHTML、TIWLayoutMgrHTML、TIWLayoutMgrForm
使用 IntraWeb (32) - Url 映射与 THandlers
万一 · 2014-06-23 · via 博客园 - 万一

最简单的 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;