Hello World, I'd like a fancier grocery
list - In the previous example we saw how a BList component can provide
the same kind of grocery list that the BTemplate generated, without adding
anything extra to the HTML template. So why would we ever want to use the BTemplate?
Well that's the purpose of this example: to demonstrate just how flexible it can be. To do
this, we've put together several template examples that all use the exact same servlet
implementation. This illustrates how the designer can drastically alter the look and feel
of the pages without needing to ask the developer to make any modifications to the
servlet.
Noteworthy Aspects in the Source
Ok, so what's worth noting in the code?
Actually, there's not much that we haven't already seen in the previous examples. As we
have already mentioned, we are again using BTemplate. The only real difference, however,
is that we are using different DOM templates, based on an optional HttpRequest parameter.
The code to load the page, then, looks something like this:
String pageNo =
req.getParameter("page");
if (pageNo==null) pageNo = "1";
Class pageCl = null;
try {
pageCl =
Class.forName("org.enhydra...xmlc.HelloWorld2b"+pageNo+"HTML");
} catch (ClassNotFoundException e) {
pageNo = "1";
pageCl = HelloWorld2b1HTML.class;
}
...
XMLObject page = xmlcFactory.create(pageCl);
This is really quite straightforward. We look for a "page" parameter in the
URL parameters and construct the name of the DOM template based on that. In real life, you
probably wouldn't ever do anything like this. We do it here to demonstrate how the look
and feel of the pages can change so drastically just by modifying the templates.
The only other points worth noting are very minor: The GroceriesModel supports one
extra parameter that was not present in previous examples ("RowCntr"); the
HelloWorld model supports a "Header" model (used to switch between l&fs).
That's about it!
Looking at the Templates
So, what about the templates? Well, in this case, none of the templates are doing
anything we haven't already seen either. The directives are all the same, it's just that
the markup containing them has changed. So in this case it's probably simplest just to
peruse the templates themselves to understand how each screen achieves the desired layout.
The key point to take away from this example is that all of these screens could have
been created by the developer without any intervention or help from the programmer. The
developer has to recompile the templates using XMLC, but that is it. This is what makes
the BTemplate approach so powerful -- it's easier for developers and designers to each
focus on their respective tasks without having to bother one another. |