Tutorial

Hello World application

Let us write a simple web application using TROIKA.ASP framework. The objective here is to test the basic setup and teach how to use the framework.

  1. Create new mapping for URI string, Command and RequestContext (form) in WEB-INF/config.xml:

    
    ...
    <req-ctx-maps>
      <req-ctx-map name="helloForm" type="HelloForm" />
    </req-ctx-maps>
    …
    <cmd-map action="helloworld" validate="true">
      <name>helloForm</name>
      <type>HelloAction</type>
      <input redirect="false" path="/views/error.xsl" />
      <forwards>
        <forward name="empty-error" redirect="false"
          path="/views/error.xsl" />
        <forward name="success" redirect="false"
          path="/views/hello.xsl" />
      </forwards>
    </cmd-map>
    ...
    

    This is what this above declaration says:

    •     Handle request with URI helloworld with Command from WEB-INF/commands/HelloAction.js e.g.
      http://localhost/troika.asp?cmd=helloworld
      
    •     Use RequestContext implementation Web-INF/forms/HelloForm.js
    •     Enable the RequestContext validation and in case of an error forward the input request to the error template /views/error.xsl
    •     There are two forward links, one to follow when HelloAction class checks for visitorName being empty. The visitorName field is one of the properties of HelloForm class. The other forward is for success view.
    •     For all views we chose to use server side forward method (redirect="false") as oppose to http redirect.
  2. Create the RequestContext and place it to WEB-INF/forms/HelloForm.js file:
    
    HelloForm.prototype =  new RequestContext();
    HelloForm.prototype.constructor = HelloForm;
    
    function HelloForm(action) {
    
        if (arguments.length) {
    
            this.init(action);
        }
    
    }
    
    HelloForm.prototype.init = function (action) {
    
        RequestContext.prototype.init.call(this, action);
    
        this.visitorName = null;
    
        return this;
    };
    
    HelloForm.prototype.validate = function () {
    
        var result =  new ArrayList();
    
        result.addAll(this.checkRequired("Name", this.visitorName));
    
        return result;
    }; 
    
    

    As you see, HelloForm extends RequestContext, defines a field called visitorName and implements validate method checking for visitorName variable value to be supplied, otherwise you will get the following error message when you click on submit button without entering name:

    Error page:

  3. Create a Command implementation – WEB-INF/commands/HelloAction.js
    
    
    HelloAction.prototype = new Command ();
    HelloAction.prototype.constructor = HelloAction;
    
    HelloAction.prototype.init = function (config) {
    
        Command.prototype.init.call(this, config);
        return this;
    };
    
    function HelloAction(config) {
    
        if (arguments.length) {
    
            this.init(config);
        }
    }
    
    HelloAction.prototype.execute = function (environment, helloForm) {
    
        var result =  new ResponseContext(this.findForward(helloForm, "success"));
    
        if (helloForm.visitorName.trim()) {
    
            result.models.put("now",  new Date().toString());
            Logger.log.info("Visitor name: " + helloForm.visitorName);
        }
        else {
    
            result.forward = this.findForward(helloForm, "action-error");
            result.errors.add(new Error("Name cannot be empty!"));
        }
    
        return result;
    };
    
  4. Create an error template WEB-INF/view/error.xsl. This template displays any errors present in XML output produced by HelloAction class. In order to access the XML right click in the html page, select View Source option.

    In debug mode there is XML section at the bottom of each pages rendered with XSL template. This section is not visible to the user and commented out.

    Say your error page looks like this:

    The available XML will be as follows:

    
    …
    </html>
    <!--<HashMap>
    ..
        <responseCtx>
          <errors>
            <elements>
              <e0>
                <name>Error</name>
                <description>Name cannot be empty!</description>
                <message> Name cannot be empty!</message>
              </e0>
            </elements>
          </errors>
    ..
        </responseCtx>
      </values>
    </HashMap>
    -->
    
    

    WARNING! In production please switch debugging off, see WWW/global.asa:

    
    Application.Contents("debug") = 0;
    
  5. Create WEB-INF/view/hello.xsl template
  6. Create static html page WWW/index.htm containing form definition:

    
    
    <form action=" troika.asp" method="post">
      <input type="hidden" name="cmd" value="helloworld"/>
      <input name="visitorName" type="text"/>
      <input type="submit" name="submit" value="submit"/>
    </form>
    
    

    This constructs a valid post request to the controller page troika-asp. If you change the method attribute to get, the valid request will look like this:

    http://localhost/troika.asp?cmd=helloworld&visitorName=Jack
    
  7. Create the bootstrap ASP page troika.asp. The page loads all classes into memory before processing the request by the framework:

    
    …
    <script language="javascript" runat="server" src="../WEB-INF/classes/forms/HelloForm.js"></script>
    <script language="javascript" runat="server" src="../WEB-INF/classes/commands/HelloAction.js"></script>
    …
    <script language="javascript" runat="server">new FrontController().process();</script>
    
    

    Note: our website is more or less one page. This page handles the incoming requests by delegating them to the TROIKA.ASP framework.

    For small/medium websites that should not be a problem. Microsoft IIS server parses any ASP page and compiles it into a p-code. This p-code can then be repeatedly executed by a virtual machine which it much faster then parsing and interpreting ASP pages every time.

    So as long as all included files (*.js files) stay the same, IIS continues to run the p-code version of troika.asp. If there are multiple requests for this page IIS creates a new thread, creates another virtual machine and clones the original p-code without reparsing the troika.asp again. This is very fast.

  8. To test the application, enter a name:

  9. You will get the output like this:

  10. Checkout the application log file WEB-INF/logs/application.log
  11. Right click on the page, choose View Source, you will get the following HTML:

    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
          Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Hello World Application</title>
    </head>
    <body>
    <h2>
              Hello
              Jack
              !
            </h2>
    <p>
              Date/Time is
              2007/03/06 02:08:23</p><a href="/index.html">Go back</a></body>
    </html>
    <!--<HashMap>
      <values>
        <requestCtx>
          <visitorName>Jack</visitorName>
          <action>helloworld</action>
        </requestCtx>
        <responseCtx>
          <errors>
            <elements>
            </elements>
          </errors>
          <models>
            <values>
              <now>2007/03/06 02:08:23</now>
              <sessionId>634289451</sessionId>
              <securityRole>guest</securityRole>
            </values>
          </models>
          <forward>
            <path>/views/hello.xsl</path>
          </forward>
        </responseCtx>
      </values>
    </HashMap>
    -->
    
  12. That is the end of Hello World Application tutorial. Congratulations!

Search

Tutorial

FAQ

Bookmarks

AddThis Social Bookmark Button

Support This Project