Best viewed with a browser
released in the 21st Century (but where possible the site is lynx-friendly) |
Home > Tech Papers > WebMacro Technical Investigation Technology Investigation Outline - WebMacroIntroductionThe purpose of this document is to explore the capabilities of WebMacro in the context of developing web-based applications. The document provides a broad overview of standard WebMacro technology, including:
A list of related online resources is provided at the end of the document.
What is WebMacro?
WebMacro, like Sun's JavaServer page (JSP) technology, is an extension of the Java Servlet API. However, unlike JSPs which must be translated into servlet classes before being executed, WebMacro pages are essentially templates which are read in, parsed and displayed via the WebMacro template engine (*rewrite*). The goal of WebMacro is to separate page design from the application code. WebMacro is being developed by Semiotek Inc and is available for use under the GNU Public Licence (GPL). A less restrictive licence can be negotiated with the developers. More details regarding licensing are contained in the WebMacro Licensing document. WebMacro has been designed to fit the biases of the developers (from WebMacro Readme document):
In keeping with it's design philosophy, WebMacro provides two separate interfaces:
How WebMacro WorksWebMacro consists of an HTML template engine and a framework of back end servlet classes. WebMacro applications operate within the standard HTTP request/response paradigm. In broad terms, a WebMacro application works as follows:
WebMacro technology enforces the separation of page design and application logic. Therefore, any application based on WebMacro will require at least two source files: a WebMacro servlet and a WebMacro template. This contrasts with "pure" servlets which allow the development of servlets that contain the application logic and generate all the HTML markup. The WebMacro approach also contrasts with JSPs, which allow developers to embed Java code into pages alongside HTML markup. The WebMacro philosophy promotes the Model-View-Controller (MVC) framework. This framework separates the model (or business logic) from the view logic in an application, with a controller managing the interaction between the model and the view. In the context of a Java-based web application, JavaBeans provide a natural mechanism for implementing the business logic. WebMacro templates can be used as the view component. And WebMacro servlets can implement the controller component. This is discussed in the WebMacro Framework Design Overview. The diagram below shows how WebMacro technology can be used within an MVC framework.
The WebMacro QuickStart document provides an overview of how to write WebMacro applications. The following section is based on information contained in that document. More comprehensive information is contained in the WebMacro API documentation and in the WebMacro Script article. As mentioned earlier, a WebMacro application consists of at least two components: a WebMacro servlet and a WebMacro template. WebMacro ServletsThe servlet component involves either implementing the org.webmacro.servlet.Handler interface, or else subclassing from org.webmacro.servlet.WMServlet. Since the latter is the preferred approach, this document will focus on that approach.
Any variable that placed into the WebContext is available as a local variable to the template. The WebContext is a JDK 1.2 style Map. Objects are placed into the WebContext by invoking the put() method. For example:
The handle() method returns a Template object. It is the responsibility of the handle() method to select which template will be used to generate the HTML that is returned to the browser. Templates use "Property Introspection" to access the data stored in the WebContext. WebMacro uses the introspection/reflection APIs to take a pretty deep look into the objects in the WebContext. The template language has access to the properties of the objects contained in the map. For simple values, it will attempt this in two ways:
If a list of objects is to be stored in the template there are several options. The property introspection method is capable of iterating through any of the following types:
WebMacro TemplatesTemplates contain the output of the application. Each template represents a one page view in the system. Any objects placed by the servlet in the WebContext can be accessed by the template. For example, if the servlet places an object called "customer" in the WebContext, the object's fields can be accessed as follows:
It is possible to loop through list values that may be part of an object: <ul> #foreach $customer in $customerList { <li> $customer.name owes $customer.owing } </ul> Simple conditionals are also supported. In the current version developers can only test whether something is defined or not: #if ($variable) { Yes we have variable: $variable } #else { Sorry, variable is not defined here } Other things that can be done using WebMacro Script in templates are:
The WebMacro context includes a collection of convenient variables that can be accessed. For example
An example application using WebMacro will be presented and described in the next section.
Developing a Form Using WebMacroTo demonstrate how to develop a form using WebMacro technology, the following example HTML template and Java program will be discussed. It's a rather simple application that prompts the user to enter a string, which is then encoded into a format that is URL-friendly. The source code for the application logic of this example is provided in Listing 1: URLEncodingWMPage.java, while the view is given in Listing 2: URLEncodingWMPage.wm.
Listing 1 is the WebMacro servlet. A line-by-line description will now be provided. Lines 1 to 10 contain some comments regarding the servlet. Lines 11 to 16 contain statements to import the required packages:
Line 17 declares that the URLEncodingWMPage class is a subclass of org.webmacro.servlet.WMServlet. Lines 19 to 21 declare some private variables used within the servlet. Line 22 declares and initialises a log for the servlet. Lines 23 to 59 define the handle() method. It accepts a WebContext object, returns a Template object and throws a HandlerException if something goes astray.
Listing 2 is the WebMacro template. A line-by-line description will now be provided. Lines 1 to 10 contain some comments regarding the template. Lines 11 to 12 contain HTML markup to begin the page and set the title. Line 13 contains a WebMacro Script directive which sets the content type of the page to "text/html". Line 14 starts the BODY section, setting the background colour. Line 15 contains a WebMacro Script comment, which will not be visible in the generated web page. Lines 16 to 23 contain a WebMacro Script conditional test and the HTML markup to generate if the condition is met:
Lines 25 to 32 contain the HTML markup to display the form.
Lines 33 to 37 contain the HTML markup to complete the page.
An example application which employs the MVC framework is provided in appendix 1.
Comparing WebMacro to JavaServer PagesThere a several major criticisms put forward regarding JavaServer page technology. In an article entitled The Problems with JSP, Jason Hunter identifies several issues, which WebMacro attempts to address:
WebMacro arguably achieves the separation of form and function using a simpler process and cleaner syntax. However, template engines generally have some downsides:
Reader reaction to Jason Hunter's article is also available online.
Developing Object Frameworks based on WebMacroWebMacro templates contain most or all of the HTML markup, with a only dynamic content provided by servlets and/or back end components. It still may be beneficial to create a framework for generating basic HTML. An object framework can also be developed to provide an abstraction over database connectivity. Assuming standard Java is used, it shouldn't be too difficult to develop object frameworks as JavaBeans which can be used in both JSP- and WebMacro-based architectures.
Other Relevant IssuesState, Authentication and ChunkingSince WebMacro technology is an extension of Java Servlets, all features available to servlet programmers are available in applications built using WebMacro. This includes the standard servlet session and cookie management capabilities. PerformanceSince WebMacro templates will need to be loaded and parsed before being presented, there will be a performance overhead the first time a page is accessed via the web server. Subsequent hits however will access the pre-parsed copy of the template. Software RequirementsWebMacro is 100% pure Java, and is an extension of standard servlet technology. Once installed and configured, WebMacro should work with any Java-based servlet engine and/or Java-enabled web server. Configuring WebMacro to work within a particular servlet environment may not always be straightforward. And the instructions provided were incomplete and contained some inaccuracies. A reasonable understanding of the servlet engine, web server and other components of the system environment is required in order to get WebMacro working properly. A support Mailing List is available to help developers get WebMacro up and running, as well as to provide assistance with WebMacro development. JDK RequirementWebMacro as supplied is immediately usable with JDK 1.1.x. If the development and deployment environment is based on JDK 1.2, it will be necessary to unpack the sources and make a handful of simple modifications, before recompiling. The process is described in the WebMacro Readme document. JavaScript IssueThere is a minor issue regarding the use of JavaScript within WebMacro templates. Specifically, developers need to be careful when using curly brackets to define blocks. The online documentation demonstrates how to safely incorporate JavaScript within templates. Status of WebMacroIt should be noted that WebMacro is currently at version 0.89.1, and is still undergoing development. However it is a widely-used and well-respected template engine. Information about the current status of WebMacro is available online.
Online Resources
Appendix 1: Camtech Intranet front pageCamtech's Intranet front page has been re-written to utilise WebMacro technology and JavaBeans. The source code for the Java-specific components is provided here. Notes:
Date: Friday, April 28, 2000 10:27 AM Home > Tech Papers > WebMacro Technical Investigation |