最简单的 Url 映射是使用 TIWAppForm 的 class 方法: SetURL;

THandlers 是 IntraWeb XIV 新增的内容处理器, 它能完成的不仅仅是 Url 映射(转发?).

THandlers 通过虚拟路径、虚拟文件名, 可以转到或处理任何文件.

这个过程中会用到一个 TContentBase 类型的参数, TContentForm、TContentRedirect 是它的子类; 有时会需要从 TContentBase 继承出解决更多问题的子类.


THandlers 所在单元及继承链:
IW.Content.Handlers.THandlers

主要成员:


{添加到内容处理器: 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;

使用 IntraWeb (32) - Url 映射与 THandlers的更多相关文章

  1. ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)

    本章将讲述ASP.NET MVC5 的路由原理,即URL映射机制. 简单点就是解释:为什么MVC在浏览器输入地址就能访问到类(或类中的方法)?这是怎么做到的?我自己可以通过.NET写出一个自己的MVC ...

  2. urlMappings与URL映射

    此配置节的作用就是往Web程序中添加URL的映射,从而达到用户访问映射后的URL(如/Page/AAA)也能访问到源URL(如/Page/PageAAA.aspx)的效果.这也是URL映射本来的作用. ...

  3. django中“url映射规则”和“服务端响应顺序”

    1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单.      查看方式:         import sys        print sys.path   ...

  4. MVC 5 Web编程2 -- URL映射

    ASP.NET MVC 5 Web编程2 -- URL映射(路由原理) 2015-02-12 08:50 by hangwei, 704 阅读, 5 评论, 收藏, 编辑 本章将讲述ASP.NET M ...

  5. CCF CSP 201803-3 URL映射

    转载自 https://blog.csdn.net/tigerisland45/article/details/81697594 /* CCF201803-3 URL映射 */ #include &l ...

  6. SpringMvc的Url映射和传参案例(转)

    Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...

  7. SpringMvc的Url映射和传参案例

    Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...

  8. Django - 将URL映射到视图

    URLconf 就像是 Django 所支撑网站的目录.它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间的映射表.你就是以这种方式告诉 Django,对于这个 URL 调用这段代码, ...

  9. 搭建RESTful API 之 实现WSGI服务的URL映射

    javarestfull 搭建参考 http://blog.csdn.net/hejias/article/details/47424511 问题引出:对于一个稍具规模的网站来说,实现的功能不可能通过 ...

随机推荐

  1. java虚拟机内存不足,“Could not create the Java Virtual Machine”问题解决方案

    在运行java程序时,遇到问题"Could not create the Java Virtual Machine."如下截图:

  2. --save-dev和--save的区别

    使用npm来进行前端包管理的时候,我们会用到npm install或者cnpm install命令来安装需要用到的包资源 1: npm install *** --save-dev 2: npm in ...

  3. SeaJS入门教程系列之完整示例(三)

    一个完整的例子上文说了那么多,知识点比较分散,所以最后我打算用一个完整的SeaJS例子把这些知识点串起来,方便朋友们归纳回顾.这个例子包含如下文件: 1.index.html——主页面.2.sea.j ...

  4. 使用siege执行压力测试

    没有安装siege? 可参考我的另一篇博客 使用siege执行压力测试笔记 场景分析 使用siege对https://www.baidu.com/进行加压. 要求 模拟20个用户同时访问 一共跑3个循 ...

  5. Grafana 监控系统是否重启

    一.概述 Linux 内核(以下简称内核)是一个不与特定进程相关的功能集合,内核的代码很难轻易的在调试器中执行和跟踪.开发者认为,内核如果发生了错误,就不应该继续运 行.因此内核发生错误时,它的行为通 ...

  6. 配置CenOS网络,并用Xshell链接。

    首先输入 cd /etc/sysconf ig/network-scripts/ 然后回车 输入ls 然后回车 输入 vi ifcfg-eth0  然后回车 按下esc键,然先后按下U,I键把光标用键 ...

  7. 数据流分段下载(Http之 Range)

    public FileStreamResult StreamUploadedSongs(int id) { byte[] song = db.UploadedSongs.Where(x => x ...

  8. js的"|"

    3|4 转换为二进制之后011|100  相加得到111=7 4|4 转换为二进制之后100 |100  相加得到1000=8 8|3 转换为二进制之后1000 |011  相加得到1011=11 以 ...

  9. C. 【UNR #2】黎明前的巧克力

    题解: 不会FWT,只能水40分了 首先,要观察出的性质就是: 选出的集合要满足所有数亦或等于0,而在其中任选子集都可以满足条件,答案就等于sigma(2^size(s)) 这样dp一波显然就可以O( ...

  10. java:给你一个数组和两个索引,交换下标为这两个索引的数字

    给你一个数组和两个索引,交换下标为这两个索引的数字 import java.util.Arrays; public class Solution { public static void main(S ...