Step 3 - Adding our own Request Method

When we look at the TestPage request we know that the process class is the one that actually handles the incoming request. The Wizard generated this class, which is essentially a template to which we can add new methods, for us.

WebProcess.prg is called from WebDemoMain.prg and is loaded via a small loader stub that creates the WebProcess class and then routes the request to the method that maps the URL. In this Testpage.wp maps to the TestPage method in the class.

* * **********************************************************************
*PROCEDURE WebProcess
* * **************************
LPARAMETER loServer
LOCAL loProcess
PRIVATE Request, Response, Server, Session, Process
STORE .null. TO Request, Response, Server, Session, Process

#INCLUDE WCONNECT.H

loProcess=CREATE("WebProcess",loServer)
loProcess.lShowRequestData = loServer.lShowRequestData

IF VARTYPE(loProcess)#"O"
   * **All we can do is return...
   WAIT WINDOW NOWAIT "Unable to create Process object..."
   RETURN .F.
ENDIF

* **Call the Process Method that handles the request
loProcess.Process()

RETURN

* ************************************************************
DEFINE CLASS WebProcess AS WWC_PROCESS
* * ***********************************************************

cResponseClass = [wwPageResponse40]     && Original generated: [WWC_PAGERESPONSE]  

* ********************************************************************
FUNCTION TestPage()
* ***********************

THIS.StandardPage("Hello World from the WebProcess process",;
                  "If you got here, everything should be working fine.<p>" + ;
                  "Time: <b>" + TIME()+ "</b>")
                  
ENDFUNC
* EOF WebProcess::TestPage

ENDDEFINE

Adding a new Request Handler Method

Any public method in this class is accessible as <methodName>.wp. So adding a new request handler is as easy as adding a new method to the class.

The following method displays a customer list from FoxPro data:

* ***********************************************************************
*  CustomerList
* ***************************************
FUNCTION CustomerList()

* Make sure Customers is in PATH \wconnect\wwdemo

SELECT company, LastName, FirstName, Phone, Entered ;
   FROM Customers ;
   ORDER BY Company ;
   INTO CURSOR TQuery      

* **Helper to generate templated HTML Header
Response.Write(this.PageHeaderTemplate("Customer List"))

Response.Write("<h3 ><i class='fa fa-list'></i> Customer List</h3>")
Response.Write("<hr/>")

lcHtml = HtmlDataGrid("TQUery")
Response.Write(lcHtml)

* **Helper to generate templated HTML Footer
Response.Write(this.PageFooterTemplate())

ENDFUNC

Then run:

http://localhost/WebDemo/CustomerList.wp

Make sure that the Web Connection server is running. When you hit the URL you should see:

Pretty cool, right? With a couple of lines of code, we've run a request on the server and have displayed the result of a FoxPro query as HTML over the Web.

Let's look a little closer at what that code does. We start with a plain VFP SQL query that generates a cursor. Next we start to build an HTML document, in this case using a helper function that generates an HTML header for us. The wwProcess::PageHeaderTemplate() method uses an on disk template that you can modify and that contains the HTML header, and in this case the banner layout on top. Everything above the Hello World text comes from this template.

Next we use a function the wwHtmlHelpers Library which includes an HtmlDataGrid() function. This function takes a cursor and turns it into a formatted HTML table. HtmlTable supports optional parameters for a configuration object that you can use to configure how the grid is configured, but here I'm just using the default rendering which simply renders a table. We'll look at more control later in this tutorial.

Finally I use the PageFooterTemplate() method to render the footer for the page which again is loaded from pre-defined project templates.

Again, I want to stress that this is just one way you can generate HTML from your data, but it's one of the quickest ways to get data displayed on the screen.

Header, Footer and Page Templates

The header, footer and page templates are loaded from files localed in the Web\Views folder. These files contain plain HTML and you can modify those templates as you see fit. These files are used for Web Connection system rendering in admin pages, but as you've seen in the previous example you can also access these templates yourself. If you want to modify the layout or text, simply change the templates in the Views folder.

Note that there's no requirement to use these templates - they are a convenience feature that is available because Web Connection requires it for its own administration pages.


Step 4 - Adding simple User Input with QueryStrings


© West Wind Technologies, 1996-2017 • Updated: 02/25/16
Comment or report problem with topic