Advanced Control Flow - Usage
Using Cocoon's Control Flow
The general flow of actions in an application which uses the control flow is as described below.
The request is received by Cocoon and passed to the sitemap for processing. In the sitemap, you can do two things to pass the control to the Control Flow layer:
- To restart the computation of a previously stopped function, you use the <map:callcontinuation="..."/> construction. This restarts the computation saved in a continuation object identified by the string value of the continuation attribute. This value could be extracted in the sitemap from the requested URL, from a POST or GET parameter etc. When the computation stored in the continuation object is restarted, it appears as if nothing happened, all the local and global variables have exactly the same values as they had when the computation was stopped.
When the business logic is invoked, you're inside the Model. The business logic takes whatever actions are needed, accessing a database, making a SOAP request to a Web service etc. When this logic finishes, the program control goes back to the Control Flow.
Once here, the Control Flow has to decide which page needs to be sent back to the client browser. To do this, the script can invoke one of the cocoon.sendPageAndWait() or cocoon.sendPage() functions. These functions take two parameters, the relative URL of the page to be sent back to the client, and a context object which can be accessed inside this page to extract various values and place them in the generated page.
The page specified by the URL is processed by the sitemap, using the normal sitemap rules. The simplest case is a generator followed by an XSLT transformation and a serializer. This page generation is part of the View layer. To process a page you can make use of several Cocoon generators to retrieve values from the context objects passed by the Control Flow.
The above explains how MVC could be really achieved in Cocoon with the control flow layer. Note that there is no direct communication between Model and View, everything is directed by the Control Flow by passing to View a context object constructed from Model data.
As hinted in the previous section, an application using Cocoon's MVC approach is composed of three layers:
- The business logic model which implements your application
- The page templates, which describe the content of the pages, and XSLT stylesheets which describe the look of the content.
An example of such an application is the user login and preferences sample
- edit and
The entry level point in the application can be any of these functions, but in order for a user to use the application, (s)he must login first. Once the user logs in, we want to maintain the Java User object which represents the user between top-level function invocations.