Web Application Opener - WebSnap (7)

zhaozj2021-02-08  267

Web Application Opener - WebSnap!

Seventh chapter, deep into adapter

The purpose of this chapter is completely the meaning of Adapter in WebsNAP and the use of them, before our use of Adapter and Adapterfield to handle simple information, such as calculating machines, and then use DataSetAdapter to proceed. But I think this is still not enough to understand the role of the entire Adapter in WebSNAP, so we will use a deep application for several adapters. After reading this section, I believe in the application of Adapter and why WEBSNAP So dependent on she will have a considerable experience.

7-1, simple Adapter: Tadapter

Tadapter is the simplest Adapter Container. We only apply her to the variable data in the program, such as a computer page, but her ability not only stops here, not only writing your own tadapter component. This technology is not Talking (That is the issue of Inside Websnap), we can use Tadapter to encapsulate different types of information, which is quite useful for us. For example, we can apply her to make similar PCHOME profile webpages. Next, let's make a small rule first. This program uses Tadapter to encapsulate TStringsValuelist, and use tadaptergrid to display her on the web page, please add a page module to In a task, set the page name to MyAdapter and select TadapTerPageProducer to PageProducer, then put it into a Tadapter and a TStringsValuelist component to this module, then we have to set the data and adapterfield within TstringsValuelist:

After setting up the displayed information, I have to join two Adapterfield to TSTRINGSVALUELIST, press Right to select Add All Fields in the field editor of TstringsValuelist:

To display the information on the web, we have to add a number of AdapterField in tadapter, and we need two Adapterfield, one is used to display the station name, and the other is used to display the URL, we will You can start writing a program code, because our purpose is to use tadaptergrid to display information in TStringsValuelist. So we have to write TAdapter.OnIterateRecords this event, and moving TStringsValueList record indicator on the inside: procedure TMyAdapter.Adapter1IterateRecords (Sender: TObject; Action: TIteratorMethod; var EOF: Boolean); var Intf: IIteratorSupport; P: Pointer; begin Intf: = Svsite as Iitratorsupport; Case Action of Itstart: INTF.Startiterator (P); Itnext: EOF: = NOT INTF.NEXTITERATION (P); ITEND: Begin Intf.Enditerator (p); EOF: = true; end; end; ;

In this event, we used a mesh: IITERATORSUPPORT, which realized this interface to support the role of the reference statement. You have to write online in Adapterfield to use the string in TStringsValuelist to use the TADAPTERGRID:

procedure TMyAdapter.AdaptSiteNameGetValue (Sender: TObject; var Value: Variant); begin Value: = AdaptNameField.Value; end; procedure TMyAdapter.AdaptSiteAddressGetValue (Sender: TObject; var Value: Variant); begin Value: = AdaptValueField.Value; end;

You will start the design network page, open the Visual Page Designer window, add an adapterform and an AdapterGrid, you will see the picture:

After the latter, we can see the results:

Very simple? This example allows us to solve Adapter's meaning. We can use tadapter to encapsulate any type of information and display information in the web page, and you can write a few events, which can be applied in many places, such as file lists, search results, or display some informal Database, etc. From this example, we can also guess TDataSetAdapter to seal the material library information and provide them to Tadaptergrid and TadapterfieldGroup display. Of course! If your purpose is just to display the information in TStringsValuelist, then you will directly put TStringsValuelist, which is used for the use of TADAPTER to seal some of the materials in tadapter. 7-2, TPAGEDADAPTER

The functionality and use of TPAGEDADAPTER are roughly the same as Tadapter, but she adds paging capabilities. The so-called paging ability refers to it. When the information in a web page is too much, we use more than more than one web page to display them, for example you are searching engine. After the results of the results, when the number of results exceeds 20-30, you will see the page below the page, using TPAGEDADAPTER we can easily make this web page. Let us use the example of a file list to show how to apply TPAGEDADPATER. Please add a Page Module to the project, set the PageName to myPageAdapter and select AdapterPageProducer to PageProducer, then put it into a TPAGEDADAPTER component, add two new two ADAPTERFIELD component to inside:

Then I have to write a TSTRINGS item, and store the file list to the TSTRINGS.

procedure TMyPageAdapter.InitializeList (AListDir: string); var sr: TSearchRec; begin FStrings.Clear; TStringList (FStrings) .Sorted: = False; if FindFirst (AListDir, faAnyFile, sr) = 0 then begin repeat if sr.Attr <> faDirectory then FStrings.Add (sr.Name '=' IntToStr (sr.Size)); until FindNext (sr) <> 0; end; TStringList (FStrings) .Sorted: = True; end; procedure TMyPageAdapter.WebPageModuleCreate (Sender : TObject); begin FStrings: = TStringList.Create; end; procedure TMyPageAdapter.WebPageModuleDestroy (Sender: TObject); begin FStrings.Free; end; procedure TMyPageAdapter.WebPageModuleActivate (Sender:. TObject); begin InitializeList (AppPath '/ * * '); END; Write related events related to TPAGEDADAPTER and Adapterfield:

procedure TMyPageAdapter.PagedAdapter1GetEOF (Sender: TObject; var EOF: Boolean); begin EOF: = not (FIndex

The last enabled Visual Page Designer adds Adapterform, Adaptergrid, AdapterCommandGroup and sets the Adapter characteristic value and AdapterAction, you can see the following picture:

The results of this program in my computer are as follows:

You can switch the page next to the page link below, or use prev, next to do the upper and lower page switches, then let us strengthen this web page into a full-featured profile, let the webpage have UPLOADFILE's ability, We have to add a new Tadapter and add a TadapterFileField inside:

It is also necessary to add an Adapteraction to execute the UPLOAD FILE:

I have to write an onfileupload event of Adapterfilefield, save the file uploaded by the user:

procedure TMyPageAdapter.AdaptUploadUploadFiles (Sender: TObject; Files: TUpdateFileList); var I: Integer; fs: TFileStream; begin for I: = 0 to Files.Count-1 do begin fs: = TFileStream.Create (AppPath '/' ExtractFileName (Files.files [i] .filename), fmcreate); fs.copyfrom (files.files [i] .stream, files.files [i] .stream.size); fs.free; end; end; to trigger this Incident, we must add the following program code in the Adapteracion.onexecute event:

Procedure TMYPAGEADAPTER.ADAPTUPLOADFILEEXECUTE (Sender: TOBJECT; params: tstrings); begin adapter1.UpdateRecords (WebContext.adapterRequest as IAditionRequest);

This scheme is the meaning of updating all Adapterfield materials in tadapter, which is triggered in this process. At the end, we only need to add the relevant components to Visual Page Designer:

After the program is executed, you can go upload the file to this website. Modular pre-set is C: / TEMP, you can set up the uploaded position. However, this program has a disadvantage that you will find that you can not find the file name you uploaded in the file after the upload file, which happens in the file name you uploaded by sorting is in your current page This is why we will write the program of the file list in Module.onActivate, which is related to the order of AdapterAction, please see the following process:

(Figure: 8)

From the process we can find that onactivate is triggered before Onexecute, which will happen to the web page that we execute after the action is in the same page, so we must change the location of the File LIST code, the ideal location is In the Module's BeforeDispatchPage event:

Procedure TMYPAGEADAPTER.WEBPAGEMODULEBEFOREDISPATCHPAGE (Sender: Tobject; Const PageName: String; Var Handled: Boolean); Begin Initializelist (AppPath '/ *. *'); END;

After the compilation, you can see the normal result. Let's join the ability to delete the file, we have two options, one is the same as the PCHOME file, showing a checkbox to let the user choose the file to delete. Press the delete file to press New to remove all the files selected. The other is that the last column of the Grid is displayed in the first file that is pressed, and the first method must use the session to store the files selected by the user, then you must write the relevant components, due to this Torked some of the component design, so I chose to wait until INSIDE Websnap will discuss this technology. Here we first choose the second way, please add a Tadaptelet in tpagedadapter: Adaptdeletefile. We have to write two events for her: ONGETPARAMS and Onexecute:

procedure TMyPageAdapter.AdaptDeleteFileExecute (Sender: TObject; Params: TStrings); var FileName: string; begin FileName: = Params.Values ​​[ 'FileName']; DeleteFile (AppPath '/' FileName); end; procedure TMyPageAdapter.AdaptDeleteFileGetParams (Sender : TOBJECT; Params: Tstrings; begin params.add ('filename =' adaptfilename.value);

OnGetParams will trigger before web display, so we use her params parameters to join the file name, so when this parameter is triggered, this parameter will be transferred to our Onexecute event, we can use this parameter value to delete this file. To fully understand the action we have to observe the program in the webpage:

You can see the red word, which is the parameter fills in ONGETPARAMS. When the useer presses the button, TadapterDispatcher will take this parameter and send it to our Onexecute event. After the operation of the design web page, this program has been completed, you already have a simple archive total, next you can meet the ability of the PCHOME personal page after combining user management with other controls, these It is your exercise. 7-3 TorpeditablePagedadapter

Originally, I have always avoided the design and component design in this article, but I think about it, this example may be quite useful for you, but I don't plan to explain the component design in this article, because I have to use one Articles can explain the interaction and coordination between all interfaces, but I really want to share this example with you, so I have a decision, I will give you, but the original code, etc., when INSIDE Websnap, explain! Remember the example of our previous section? Basically look very good, but we must admit that she is not very practical, not enough to inspect your other Lenovo, so I have completed this example, to compile this example, you must To install orpDemo.bpl, there is a TorpedITablePageDadapter component. She is based on TPAGEDADAPTER and adds the ability to programmed. We use her to complete the examples of unfinished in our previous section. This archive can make the user first pick To delete the information, then press the deleted button to delete the information, this is not only available to delete the file, you can also use her to choose the files selected to compress the user to download, or a shopping cart, or More places! This is why I will break her reason in this article, I am too deep, let me ignore! Complete sample registration in umysessionPagedadapter Unit, I only picked out several parts to explain Please look at it, first we use TorPedITablePageDadapter to replace the TPAGEDADAPTER list. The program of the case is the same as the season of the previous section, and we will first explain the parties for processing session:

function TMySessionPagedAdapter.AddToList (Value: string): Boolean; begin Result: = True; if not FindSession (Value, foNothing) then begin if not FindSession ( '', foNothing, Value) then begin try VarArrayUnLock (FSessionVariant); VarArrayRedim (FSessionVariant , FDeleteArraySize 1); FDeleteArray: = VarArrayLock (FSessionVariant); FDeleteArray [FDeleteArraySize 1]: = Value; Inc (FDeleteArraySize); except Result: = False; end; end; end; end; function TMySessionPagedAdapter.FindSession (Value: String; addValue: string = '): boolean; var i: integer; bfind: boolean; begin bfind: = false; if fdeleterraysize <> -1 the begin for i: = 0 To fdeletearaysize do beginix NOT VARISEMPTY (FDELETEARRAY [I])) and (value = fdeleteaRray [i]) THEN BEGIN BFIND: = true; if FindOperation = Fode Then fdeleteArray [i]: = unassigned; Break; end; if (value = ') and (addValue <>') and (fdeleteArray (fdeleteaRray [i])) The begin bfind: = true; fdeleteArray [i]: = addValue; Break; end; End; result: = bfind; end; addtolist and findsession are used to handle the SESSION program, we use a Variant Array to store the file name selected by the user, FDELETEARRAY is an array indicator, we will first operate Variant Array Use VararrayLock to speed up the speed of Variant Array.

procedure TMySessionPagedAdapter.WebPageModuleBeforeDispatchPage (Sender: TObject; const PageName: String; var Handled: Boolean); begin InitializeList (AppPath '/ * *.'); FSessionVariant: = Session.Values ​​[ 'DeleteList']; if VarIsEmpty (FSessionVariant) then begin FSessionVariant: = VarArrayCreate ([0,0], varVariant); FSessionVariant [0]: = Unassigned; Session.Values ​​[ 'DeleteList']: = FSessionVariant; FDeleteArraySize: = 0; end else FDeleteArraySize: = VarArrayHighBound (FSessionVariant, 1 FdeleteArray: = VARARRAYLOCK (FSESSIONVARIANT); END; we chose to remove the session and LOCK in the FDELETEARRAY.

procedure TMySessionPagedAdapter.WebPageModuleAfterDispatchPage (Sender: TObject; const PageName: String); begin VarArrayUnLock (FSessionVariant); Session.Values ​​[ 'DeleteList']: = FSessionVariant; end;

After Dispatch, I saved the Variant UNLOCK back into the session.

Procedure TMYSESSITIONPAGEDADAPTER.ADAPTDeletegetValue (Sender: Tobject; var value: boolean); begin value: = findsession (adaptfilename.value, fonothing);

In this example, we provide Checkbox behind each piece of information, this value must be taken from the session.

Procedure TMYSESSIONPAGEDADAPTER.ADAPTDeleteUpdateValue (Sender: Tobject; Value: Boolean); begin addtolist (fstrings.names [orpeditablepagedadapter1.currentupdateIndex]) end;

When the user selects a file or switch to other Page, we want to execute an Adapter's UpdateRecords. She triggers the AdapterField's UpdateValue event. Here we use the unique properties of TorpedTablePageDadapter: CurrentUpdateIndex, this property will tell us what we are currently updated Record position, we take the corresponding file name by fstrings.

procedure TMySessionPagedAdapter.ActionPrevPageBeforeExecute (Sender: TObject; Params: TStrings; var Handled: Boolean); var I, iClearStart, iClearEnd: Integer; begin FSessionVariant: = Session.Values ​​[ 'DeleteList']; FDeleteArray: = VarArrayLock (FSessionVariant); iClearStart : = OrpEditablePagedAdapter1.RecordIndex; iClearEnd: = OrpEditablePagedAdapter1.PageSize OrpEditablePagedAdapter1.RecordIndex; if iClearEnd> (FStrings.Count-1) then iClearEnd: = FStrings.Count-1; for I: = iClearStart to iClearEnd do FindSession (FStrings.Names [I], foDelete); OrpEditablePagedAdapter1.UpdateRecords ((WebContext.AdapterRequest as IActionRequest)); VarArrayUnlock (FSessionVariant); Session.Values ​​[ 'DeleteList']: = FSessionVariant; end; you can find sample code, I would Goto, PREV, NEXTPAGE three action BeforeeExecute events point to this event, this is to make the user easy to operate, no need to press another to update the selected file.

procedure TMySessionPagedAdapter.AdaptDeleteFileExecute (Sender: TObject; Params: TStrings); var I: Integer; begin FSessionVariant: = Session.Values ​​[ 'DeleteList']; FDeleteArray: = VarArrayLock (FSessionVariant); OrpEditablePagedAdapter1.UpdateRecords ((WebContext.AdapterRequest as IActionRequest )))); For i: = 0 to fdeleterraysize do beginiff (not varisempty (fdleteaRray [i])))) and (fdeleteArray [i] <> ') THEN Begin deletefile (AppPath ' / ' fdeleteArray "; fdeleteaRray [I]: = unassigned; end; end; fsessionVariant: = varVariant; session.values ​​['deletelist']: = fsessionVariant; end;

The top three rows are updated for session data. The latter program is to perform the action of deleting the file. In fact, this example is not very complicated, but she shows the session application, which is quite useful to we write other programs, You can find that the program written in PrevPageBeForeExecute will clear the options in the page before performing action. To understand why I wrote this, you have to use logs in WebAppdebuger to observe when you have selected a certain Post-POST information, and when you remove a selected POST data, there will be answers after the two-phase control. (PS: CHECKBOX never send false) This chapter

In this section, we understand the positioning and various applications of Adapter in Websnap, and discuss the meaning of some of the Action events, and combined with these technologies to build a simple archives, we have introduced nearly 80%. WebSnap components.

PS: The little heart mistakenly deleted some files!!

转载请注明原文地址:https://www.9cbs.com/read-3108.html

New Post(0)