Actions

Any defined prototype function with its name ending to '_action' becomes automatically web accessible to any HTTP client (which is generally a webbrowser), and is referred to as 'action' in this documentation. The following example defines two such actions:

### Root/functions.js ### 
 
function main_action() { 
  res.write("The main action of the Root object."); 
} 
 
function hello_action() { 
  res.write("Hello"); 
} 

By requesting the URL http://localhost:8080/appname/main the first defined action is being executed. A http://localhost:8080/appname/hello would call the second action. Actions that are named 'main' are also taken as the default action, if no particular action is being specified in the request path. Trailing slashes in the request path are ignored. Therefore, http://localhost:8080/appname, http://localhost:8080/appname/, http://localhost:8080/appname/main and http://localhost:8080/appname/main/ all result in the same action call.

Note, that function names, and therefore also action names are case-sensitive, i.e. http://localhost:8080/appname/main and http://localhost:8080/appname/Main are not resulting in the same action call.

Since the dot ('.') is not allowed to be part of a function name in JavaScript, an underscore needs to be used in order to define an action containing a dot. The root action 'style_css_action' would therefore be accessible as ' /appname/style.css', as well as ' /appname/style_css'. Being able to specify dots in action names is important, since there are still browser in use, that try to guess the content type of a response from the extension of the requested 'file'.

Helma provides a special function named onRequest(), which can be defined for each prototype. This function will be automatically called by Helma before any action is actually processed for a prototype. Therefore that function is a good place to implement functionality that the developer wants to make sure to be always called, like checks for access privileges.

Example:

 ### Root/functions.js ### 
 
function main_action() { 
  res.write(" and Goodbye. "); 
} 
 
function onRequest() { 
  res.write("Hello"); 
}

Calling http://localhost:8080/appname/main will result in the string 'Hello and Goodbye' being returned.