WEB programming with Intraweb (2)

zhaozj2021-02-17  54

"Delphi from the entry to the proficiency" Chapter 21

Create intraweb applications

Create an intRAWEB application, there are many components available. Let's take a look at the IW Standard page in the Delphi component board, you will leave you a deep impression, from your simple buttons, check boxes, radio boxes, edit boxes, list boxes to charming tree controls, menus, timers , Tables and links should have €, I don't want to exemplify the usage of each component. I just want to use several examples, explain the Intraweb architecture, of course, will also introduce the components used.

I created a routine (called iWtree), demonstrating the usage of Intraweb's menus and tree controls, and how to dynamically create components. The Intraweb menu works by introducing the content of the regular Delphi menu, which is easy to simply set AttachedMenu to TMENU components:

Object mainmenu1: TmainMenu

Object Tree1: TMenuItem

Object Expandall1: TMenuItem

Object Collapseall1: TMenuItem

Object N1: TMenuItem

Object Enlargefont1: TMenuItem

Object Reducefont1: TMenuItem

end

Object About1: TMenuItem

Object Application1: TMenuItem

Object treecontents1: tMenuItem

end

end

Object IWMenu1: TiwMenu

AttachedMenu = MainMenu1

Orientation = IWOHORIZONTAL

end

The onclick event is processed when the menu item is run, which is implemented in the form of a link. Look at Figure 21.3, this is the running effect of the demise menu. Another component in the routine is a tree control, and many nodes are presented in the example. This component implements a lot of JavaScript code (not required to call server code) to directly control the expansion and arrest of the tree control node. At the same time, there is also a menu item that controls the tree control node to expand and overlap and control the font size. The following is listed below:

Procedure TFormTree.expandall1Click (Sender: TOBJECT);

VAR

i: integer;

Begin

For i: = 0 to iWtreeView1.Items.count - 1 DO

IwtreeView1.items [i] .expanded: = true;

END;

Procedure TFormTree.enlargefont1click (Sender: TOBJECT);

Begin

IwtreeView1.font.size: = iWtreeView1.font.size 2;

END;

Figure 21.3 Demo menu, tree control, and dynamically created components

Thank Intraweb offers the characteristics of the standard Delphi VCL component, making the code easy to read, and it is easy to understand.

The menu in the routine has two submenus a little more complicated. The first is to display the application ID, which is the session period ID at the time of execution. This flag can be obtained by the AppId property of the global object WebApplication. The second submenu is Tree Contents that lists the number of top nodes and child nodes of the tree control. It is worth noting that this information is displayed in the component Memo created in a run (see Figure 21.3), which is very similar to the same thing as in a VCL application.

Procedure TFormTree.treeContents1click (Sender: TOBJECT); VAR

i: integer;

Begin

With TiwMemo.create (Self) DO

Begin

Parent: = Self;

Align: = Albottom;

For i: = 0 to iWtreeView1.Items.count - 1 DO

Lines.add (iWtreeView1.Items [i] .caption '('

INTTOSTR (iWtreeView1.Items [i] .subitems.count) ');

END;

END;

Tip: Please note that intraweb's Alignment property is very similar to the Alignment of the VCL component. For example, the alignment attribute of the program menu is Altop, and the Tree component is AlClient, and the dynamically created MEMO's Alignment property is set to Albottom. As an alternative method, you can use ANCHORS (also in the same VCL): You can create a bottom-right button, or the components in the page, just set the four Anchors of the component to True. Please see the routine about this technology below.

Writing Multipage Applications

Develop multi-page applications

The routines currently have only have a page, let's create the second page of the intraweb program. In fact, this is the case, Intraweb development tools are also very similar to standard Delphi (or Kylix), and different from other Internet development tools. The following example can automatically generate source code via the intraweb wizard, followed by these code we care.

From the beginning, the main form of routine IWTWOFORMS demonstrates the intraweb table features. This is a powerful component that she generates the HTML form that can be placed either in the text and placed in other components. In this example, the program starts running, the contents of the table (processed in the oncreate event of the main form):

Procedure tFormMain.iwappformcreate (sender: TOBJECT);

VAR

i: integer;

Link: Tiwurl;

Begin

// set Grid Titles

IWGRID1.Cell [0, 0] .Text: = 'row';

IWGRID1.Cell [0, 1] .Text: = 'Owner';

IWGRID1.Cell [0, 2] .Text: = 'Web Site';

// set Grid Contents

For i: = 1 to ion rowcount - 1 do

Begin

IWGRID1.Cell [I, 0] .Text: = 'ROW' INTOSTR (i 1);

IWGRID1.Cell [I, 1] .Text: = 'IWTWOFORMS by marco cantù';

Link: = Tiwurl.create (Self);

Link.Text: = 'Click Here';

Link.url: = 'http://www.marcocantu.com';

IWGRID1.Cell [i, 2] .control: = link;

END;

END;

The results of the above code are shown in Figure 21.4, in addition to the output, there are still a few things worth noting. First, the table uses the Delphi's Anchors property (set to false) to enable the table to always in the page, even if the user changed the browser window size. Second, I added an iWURL component in the third column, of course, other components (including buttons and editing boxes). Figure 21.4 Range IWTWOFORMS Table Components Used in Text and iWURL Components

Third, it is also the most worth studying that the IWGRID component is converted into a frame and a frame-free HTML form. This is a html code snippet in the table:

ROW 2

IWTWOFORMS by marco cantù

ID = "tiwurl1" name = "tiwurl1"

Style = "z-index: 100; font-style: Normal; Font-size: 10pt; Text-Decoration: none;">>>>>>>>

Click Here

Tip: In the code list above, we noticed that the URL is activated through JavaScript, not a direct hyperlink. Since intraweB allows all the actions, such as confirming, checking, and commit, and these actions are dependent on JavaScript. For example, if you set a component's Required to true, the component cannot be submitted if there is no data, and if the submission will see a JavaScript error message (the message box using the component's FriendlyName property).

The core feature of this example is the ability to display the second page. In order to achieve this, first, you need to add an intraweB page to the program. The method is to click File-> New-> Other ..., start the Delphi's New Items dialog, turn it to the intraweb page, select Application Form, click the "OK" button. Complete the addition of work. Then put some components to the form, then place a button or other control in the main window to display the second form:

Procedure TFormMain.btnshowGraphicclick (Sender: TOBJECT);

Begin

Anotherform: = tanotherform.create (WebApplication);

Anotherform.show; end;

Even if the program calls the Show method, it will also be called to call ShowModal. This is because Intraweb handles the page as a stack. The last displayed page is on the top, and it is displayed on the browser. If you close this page (hidden or destroyed), you will display the previous page of the page. In this example, the closure of the second page is by calling the Release method, the method is the proper method of ending the running form in the VCL program. You can also hide the second form and then display it to avoid instances of the form.

Warning: A Close button is placed on the main form in the routine, but the button does not call the Release method, but call the Terminate method of the WebApplication object. This method can pass output information, such as WebApplication.Terminate ('Goodbye'); use another alternative method: TerminateAndredirect.

Now I have known how to create an intraweb program with two forms, then we briefly examine how intRaweb is created. When you create a new program, there is a related code generated by the intraweb wizard in the project file:

Begin

IWRUN (TFormmain, TiwServerController);

This line is different from Delphi standard project file because it calls a full-class function instead of the application of global objects. The two parameters of the function are the class of the main form and the class of the intraweb controller. The controller can handle the session and a number of features, will be described later.

The second form in the routine displays another interesting feature of intraweB: graphics support. The form has a graphic component that displays the Athena God, which is implemented by loading a bitmap into a iWImage component: intRAWEB converts this bitmap into a JPEG format, and saves Cache created in the folder where the program is located In the folder, then the reference to the JPEG file is returned. Its corresponding HTML code is as follows:

An additional feature used by this routine is that the user can click on the image with the mouse and function to modify the image by running the server-side code. In this example, the modified result is a green small circle.

code show as below:

Procedure Tanotherform.iWimage1Mousedown (ASENDER: TOBJECT)

Const AX, AY: Integer;

VAR

Acanvas: Tcanvas;

Begin

Acanvas: = iwimage1.picture.bitmap.canvas;

Acanvas.pen.width: = 8;

Acanvas.pen.color: = CLGREEN

Acanvas.ellipse (AX - 10, AY - 10, AX 10, AY 10);

END;

Warning: Drawing operations occur on Canvas in a bitmap. Do not use the image of the image component (which can be done in the VCL component image), do not use the JPEG image, otherwise it is not a response is running error.

Session period management

Note: The session is a call, the word road. When you call, usually every pair of users have a word, otherwise it will "slam". In this chapter, Session refers to a user-interactive voice, or a interactive channel. This translation is used here due to other books. - Translator. If you have some web programming experience, you will know that the session management is a complex topic. Intrawe provides predefined session period management and simplifies how to use the session period. If session data is required in a specified form, you need to do a domain to the form. Intraweb forms and components create an instance for each session period. For example, in the routine iWssion, I add a domain to the form called Formcount. For comparison, I declare a global variable globalcount in the global unit, which is shared by all instances of the program.

In order to enhance control of session data, you can customize the TUSERSESSION class, which can customize the TUSERSESSION class, which is automatically generated in the Intraweb application wizard in the ServerController unit. In the routine iWssion, I am customized:

Type

Tusersession = Class

public

Usercount: integer;

END;

IntraweB creates an instance of an object for each new session period, see the IWServerControllerBasenewSession method for TiwserverController classes in the ServerController unit:

Procedure TiwServerController.iWserverControllerBasenewSession

ASESSION: TIWAPPLICATION; VAR VMAINFORM: TIWAPPFORM;

Begin

ASESSION.DATA: = TUSERSESSION.CREATE;

END;

In the code, the session period can be referenced by accessing the DATA domain of the RWEBApplication this global variable, which is usually used to access the current user's session period.

Tip: rwebApplication is a thread variable and is defined in the iWinit unit. She provides a thread security method for accessing the sessional data: I need to be doubeled in multithreaded environments. This variable can be used outside of the form and control (based on thread), which is why it is mainly used in data modules, global procedures, and non-intraweb classes.

In addition, the default ServerController unit provides an available auxiliary function:

Function UserSession: tusersession;

Begin

Result: = tusersession (rwebapplication.data);

END;

Because most of the code has automatically generated, just like the code extracted from the routine IWSession, simply applying the TUSERSESSION class to the Tusession function. In the routine, click the button, the program will accumulate several counters (a global variable, the two session period specified) and display their values ​​through the label:

Procedure TFormMain.iwbutton1click (Sender: TOBJECT);

Begin

InterlockedIncrement (globalcount);

Inc (formcount);

INC (userSession.userCount);

Iwlabel1.text: = 'Global:' INTOSTOSTR (Globalcount); iwlabel2.text: = 'form:' INTOSTR (Formcount);

Iwlabel3.text: = 'User:' INTOSTR (userSession.usercount);

END;

Note that the program avoids access conflicts with global variables shared by multi-thread by calling Windows InterlockedInCrement. This can also be avoided by using critical section or TidthreadsaFeinteger (see Idthreadsafe unit).

Figure 21.5 shows the output of the program (created two session periods through two different browsers), and the program has a check box for activating the timer. It sounds very incredible, but in actually in the intraweb program, the timer works as the timer in Windows. When the time interval of the timer expires, the corresponding code will be executed. In the web page, this means triggering the JavaScript code to refresh the page:

Iwtimer1 = setTimeout ('Submitclick ("IWTIMER1", "", FALSE)', 5000);

Figure 21.5, IWSESSITION routines running in two different browsers

Integration with WebBroker and WebSnap

So far, only how to create a Stand-Alone mode of intRAWEB application. The situation is basically the same when you need to develop IIS or the intraWeb dynamic link library under Apache. However, if you want to use intraweb technology to expand existing WebBroker (or WebSnap) programs, the situation is different.

The bridges of the two technologies are IWPageProducer components. This component is attached to WebBroker's action as other page generator components, and you can use a special event to create and get an intRawEb form:

Procedure Twebmodule1.iWPageProducer1Getform (Asender: TiwPageProducer)

AWEBAPPLICATION: Tiwapplication; VAR VFORM: TIWPAGEFORM

Begin

VFORM: = TFormMain.create (awbApplication);

END;

Only one line of code, you can implement the IntrawEB page into the WebBroker program, and in the routine CGIINTRA. IWModuleController provides core services to intraweb support. Each intraweb project must have such components to work correctly.

WARNING: IWModuleController components issued in Delphi7 conflict with conflict, but the problem has been resolved and can be updated free of charge.

This is the web module form summary of the routine:

Object webmodule1: twebmodule1

Actions = <

Item

Default = TRUE

Name = 'WebActionItem1'

PathInfo = '/ show'

ONACTION = WebModule1WebactionItem1Action

end

Item

Name = 'WebActionItem2'

PathInfo = '/ iWdeMo'Producer = iWPageProducer1

End>

Object IWModuleController1: TiwModuleController

Object IWPageProducer1: TiwPageProducer

ONGETFORM = IWPAGEPRODUCER1GETFORM

end

end

Because this is a page mode CGI program, there is no session management. In addition, the component status of the page cannot be updated by the event processing as the standard Intraweb program. In order to achieve the same effect, you need to write a special code to further process the parameters of the HTTP request. You can see that the page mode without programming is so automated only from such a simple routine. However, the page mode is more flexible. It is particularly worth mentioning that page mode adds WebBroker and WebSnap programs to increase visual design capabilities.

Control layout

Randics CGIINTRA also show another very interesting intraweb technology: HTML-based layout control (there is no relationship of webbroker and layout control, because there is also a layout control in program mode, I just use one routine Note these two techniques). The result of the program-compiled result page is a mapping of a series of components on the form when designing, can change the appearance of the page by modifying component properties. There are a lot of content on a page, such as text boxes, buttons, and pictures, etc., how do these contents distribute, how to control size and location?

The solution is to use intraweb's layout manager. In the intraweb program, you always use the layout manager. The default layout manager is iWLAYOUTMGRFORM, the other two is iWtemplateProcessorHTML and iWLayoutMGRHTML, the former uses the external HTML template file, the latter embeds HTML.

The iWLayoutmgrHTML component includes a powerful HTML editor where you can embed an intRAWEB component like placing a normal HTML element (in the external HTML editor, you must manually implement). Also, when you select an intRAWeb component from your editor (Double-click the IWLAYOUTMGRHTML component, you can start the editor), you can use the object viewer to modify the component properties. As shown in Figure 21.6, intraweb's HTML layout editor is a powerful visual HTML editor that generates HTML code can be seen in another page (Source page).

Figure 21.6: Intraweb's HTML layout editor

In the generated HTML code, the structure of the page is defined. Components are identified by special marks: braces, as follows:

{% iwlabel1%} {% iWbutton1%}

Tip: When you use HTML, the component is not locked by the absolute position, but is fixed by HTML. Therefore, the form at this time is merely a component container because the position and size of the components in the form are ignored.

Anyway, the layout manager can always meet the appearance requirements of the program during the browser.

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

New Post(0)