Java Tutorial - Java Script :
WebMacro
WebMacro is an extremely popular Web-development framework that has been developed by Semiotek Inc. and is available for use under the GNU GPL and/or the Semiotek Public License. The design concept for WebMacro, like that of Tea, is to enforce the separation of presentation code from data acquisition code. This separation is accomplished through the use of the WebMacro template language, which allows designers to embed macros into their page design. The macro expansion process is performed by a servlet that extends the org.webmacro.servlet.WMServlet class. This WebMacro servlet processes user requests, acquires data from JavaBeans and other sources, and loads the data into the WebMacro WebContext, where the data is visible to
the WebMacro template. The WebMacro servlet then loads the template containing the embedded macros, and expands the macros before presenting the rendered HTML back to the browser. A summary for WebMacro is shown in the following table.
The WebMacro environment used for the examples presented in this chapteris the latest as of this writing, version1.1. Following are the basic steps needed to install WebMacro:
Unzip the WebMacro distribution file into a working directory (not in TOMCAT_ROOT\webapps).
Create the following directories:
<tomcat_root>\webapps\wm
<tomcat_root>\webapps\wm\WEB-INF
<tomcat_root>\webapps\wm\WEB-INF\classes
<tomcat_root>\webapps\wm\WEB-INF\lib
Copy WebMacro.jar from the distribution archive, and put it into the TOMCAT_ROOT \webapps\wm\WEB-INF\lib directory.
Compile and install the HelloWorld.java sample application located in the wm_examples directory by putting the class file into the TOMCAT_ROOT \webapps\wm\WEB-INF\classes directory.
5. Copy the helloWorld.wm templates file in the TOMCAT_ROOT \webapps\wm directory
. We are now ready to test our WebMacro environment by running the HelloWorld example:
Your browser should look like Figure 6.7 now, proving that the WebMacro environment is functional.
Moving on, let’s take a look at the HelloMyCity program we used in the previous section and see how to use WebMacro to accomplish the same task. First, we will create our template file, HelloMyCity.wm, and put it into our application root TOMCAT_ROOT \webapps\wm.
<html>
<head><title>Hello My City!</title></head>
<body>
#set $Response.ContentType = “text/html”
<h1>Hello $myCity!</h1>
</body>
</html>
Notice that the first and last line of our template is simple HTML, and as such it will simply be rendered as is without modification to the browser. The second line is a directive to set the content type, informing the browser what type of document it is receiving. The third line is a small demonstration of WebMacro’s macro-substitution capability. The myCity variable is put into the WebContext by the WebMacro servlet and is expanded when the template is processed. Following is the source for the WebMacro servlet to help you get a better understanding of what is going on:
// Import the WebMacro core interfaces.
import org.webmacro.*;
// Import the WebMacro servlet classes.
import org.webmacro.servlet.*;
public class HelloMyCity extends WMServlet {
public Template handle(WebContext context) throws HandlerException {
String myCity = context.getRequest().getParameter(“myCity”);
if(myCity == null)
myCity = “Fort Myers ”;
context.put(“myCity”, myCity);
Template view;
try {
// Get the “HelloMyCity.wm” template.
view = getTemplate(“HelloMyCity.wm”);
} catch (ResourceException e) {
throw new HandlerException(
“HelloMyCity loaded and executed OK, but was unable to load \n”
+ “the HelloMyCity.wm template”, e);
}
return view;
From the WebMacro servlet code we see that we our servlet is subclassed by extending WMServlet, which derived from HttpServlet, thus we inherit the getParameter() method used to extract the myCity parameter that was passed by the browser. After testing for a null condition and putting our value into the WebContext, we return the expanded view to the browser. Execute the following command to take a look at the finished product:
The result is shown in Figure 6.8.
The versatility and power of the WebMacro environment is truly impressive, but a more thorough discussion is outside the scope of this book. Considering the simplicity and power of WebMacro, coupled with the widespread following it enjoys, the application framework can be considered a desirable development environment on those merits alone. WebMacro also seems to be considerably easier to learn, set up, and use than Tea.
