通过网盘分享的文件:webBroker_ALL_6.rar
链接: https://pan.baidu.com/s/1SN_O47h8QoempWvKiSX2HQ 提取码: 39xg

通过PageProducer1连接前后端原理,不用Producer反而更加 方便 简洁,可以提升HTML,CSS,JS能力

WebBroker_PageProducer1_前端_及_结果返回替换


 WebBroker使用心得,简单收发服务器

image

,WebModule1就是一个WebDispatcher1类型

 

1】没有前端,只能自己写,只能通过Response.Content传回 前端HTML

2】前端HTML中

<script src="https://cdn.bootcdn.net/ajax/libs/quill/2.0.2/quill.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/quill/2.0.2/quill.snow.css" rel="stylesheet">

如果网络通,可以直接引用,

内网要引用外网js,CSS文件, 先将该js,CSS文件 下载到本地,再将 js,CSS文件 嵌入 到 前端HTML ,再导入Response.Content 

2】前后端交互,也要自己写

前端-------->后端

 <input type="text" name="html" id="htmlInput">

后端   接收

  S :=  Request.ContentFields.Values['html'];

后端----->前端

   Response.ContentType := 'text/html; charset="UTF-8"';
   Response.Content := S+'--测试';

 3】一个路径名,代表一个执行过程

delphi webbroker路径的含义是对应的执行动作,而不是目录名,还可通过URL传递参数

 

4】一个页面打开另外一个页面Response.SendRedirect('Show');//跳转到 Show页面

5】要注意 跨域和 UTF-8中文乱码问题

Response.CustomHeaders.Values['Access-Control-Allow-Origin'] := '*';//跨域
Response.ContentType := 'text/html; charset="UTF-8"';               //UTF-8中文乱码问题

 一、建好项目

1.新建 File-New –Other – 找到Web服务器应用程序

2.然后会弹出这些界面,基本都直接下一步就好了【这个Demo就是这样这样的】

下面可以测试一下自己的 8080 端口是否被占用了,占用了就换别的就好了,然后完成【不会影响后面的操作】

 二、项目创建完成后

然后就会看到这样的已经成型的东西

 这个时候其实我们已经实现了请求,直接运行程序

 

然后会发现访问的就是WebModuleUnit1这个单元下最后面返回的内容,接下来就是修改WebModuleUnit1这个单元里面的内容了,没有动FormUnit1单元下面内容
下面的代码是WebModuleUnit1修改后的内容

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  mPath, mXY: string;
begin
  // 设置返回的类型 【也可以写在这里,下面的就不用写】
  // Response.ContentType := 'application/json; charset="UTF-8"';
  // 获取请求路径
  mPath := Request.PathInfo;
  // 获取请求字段的值
  mXY := Request.QueryFields.Values['XY'];

  // 当访问的是首页的时候【http://localhost:8080/】这个端口号是跟前面窗体上面启动服务的端口号相同的
  if mPath = '/' then
  begin
    Response.ContentType := 'text/html; charset="UTF-8"';
    // 这个下面的是不是很熟悉,这个就是在创建后生成的返回
    Response.Content := '<html>' +
      '<head><title>Web Server Application</title></head>' +
      '<body>Web Server Application【这是默认的页面】'
      +'<a target="_blank" href="http://www.cnblogs.com/tulater">涂磊的小作</a>'
      + '</body></html>';
  end
  // 【http://localhost:8080/xaioyin】
  else if mPath = '/xiaoyin' then
  begin
    // 判断请求的参数是否是符合要求 【http://localhost:8080/xiaoyin?XY=xiaoyin01】
    if mXY = 'xiaoyin01' then
    begin
      Response.ContentType := 'application/json; charset="UTF-8"';
      Response.Content := '{"status":200,"Hint":"可以调用就表示成功啦!"}';
    end
    else
    begin
      Response.ContentType := 'application/json; charset="UTF-8"';
      Response.Content := '{"status":201,"Hint":"进来这个里面就表示参数不对给的返回"}';
    end;
  end
  else
  begin
    Response.ContentType := 'text/html; charset="UTF-8"';
    Response.Content := '<html>' +
      '<head><title>路径不对跳转的页面</title></head>' +
      '<body><font color="red">【路径不对跳转的页面】</font></body>' + '</html>';
  end;
end;

 修改后的运行结果: