Chapter 4. IMHO Objects

Table of Contents
4.1. Defining Elements
4.2. Creating Instances

4.1. Defining Elements

All objects that can be displayed on a client browser in IMHO derive in some way from html-element. IMHO objects are ordinary CLOS objects, and can be defined, redefined, and created using standard CLOS syntax. Classes for basic elements can be defined by subclassing html-element. Once a class has been defined, it can be used to 'display' part of a response to an HTTP request. Here's a very simple IMHO application:

(defclass hello-world (html-element)
  )

(defmethod render-html ((element hello-world) stream)
  (format stream "Hello World!"))

(defapplication hello-world-app
  :base-url "hello"
  :initial-element hello-world)

If the mod_webapp is configured to serve applications under "/imho" (the so-called 'mount point'), then once running, this application could be invoked at a URL that looks like this:

http://127.0.0.1/imho/hello
There are only a few things necessary to set an application running: you need to specify where it can be found ('base-url'), and what element ought to be displayed when the application receives its first request from a client. These two bits of information are declared using 'defapplication'. Once that's done, the only remaining thing to do is to tell imho to start serving the application, by invoking: (imho:init/application 'hello-world-app :start)

Note: The first time you start up any IMHO application, IMHO will attempt to bind to imho::*lisp-server-port* in order to recieve requests from your JServ adaptor. If you started a Java VM to handle JServ requests, you should kill it, or move it to another port, or configure your JServ mount point for IMHO and imho::*lisp-server-port* to use a different port for their communications.

This main element of this application, 'hello-world', has the single requisite superclass, html-element, but no other interesting characteristics. The only other code required is a 'render-html' method, to provide the element with a means of displaying itself. 'render-html' is the function that is called whenever IMHO is asked to display a component. To provide 'render-html' for 'hello-world', you define a method for this function, where the first parameter is of type 'hello-world', and the second is a variable that will be bound to the response stream at the time it is displayed.

To provide display behavior via a template, a file named "hello-world.html", containing the single line:

Hello World!
must exist in the template directory of the application.