Adding the Final Touch
Now that we have almost everything, we can add a small HTML file
so we have a starting place to check all the operations.
- The starting place will be the file Test/index.html,
which we will create by copying this HTML code:
<html>
<head>
<title>Test application</title>
</head>
<body>
Application to test the possibilities of
WebLEAF 3.0b + Groovy + HSQLDB
<br />
<ul>Test these urls:
<li>
<a href="showItems.fm?showXMLSource=true">
Listing items in the database as XML with Groovy
</a>
<ul><b>Notes</b>
<li>The LABELS node comes from the file at
<i>WEB-INF/xml/Labels.xml</i>
</li>
<li>The ITEM_QUERY node is generated using the Groovy script at
<i>WEB-INF/scr/Item.groovy</i>
</li>
</ul>
</li>
<li><a href="showItems.fm">
Listing items in the database with Groovy + FreeMarker
</a>
</li>
<li><a href="showItems.xsl">
Listing items in the database with Groovy + XSLT
</a>
</li>
</ul>
</body>
</html>
- We can now access the URL
http://localhost:8080/test/ to access the raw XML and
both views' versions, as displayed in Figure 4:

Figure 4. Snapshot of the application's default page
This is an example of how one can configure a powerful and
dynamic development environment while keeping a healthy
"separation of concerns," thanks to the new scripting
capabilities included in Java 6.
Conclusions
Now that we have everything configured, we can play directly
with the business logic (Item.groovy) or with the views
implementations (Item.ft/Main.ftl or
Item.xsl along with Labels.xml). There is no need
to recompile anything or restart the servlet context, and our
changes are picked up automatically. Moreover, if we add new
XMLOPERATION nodes, we can see that the WebLEAF configuration is
reloaded automatically (it is checked every minute by default). So
we are able to add new functionality, change the business logic, and
modify the user interface without restarting anything.
Thanks to using XML, we can reuse the libraries we already have
in the Java world and produce, for example, dynamic images using
SVG and Batik , PDF reports using
JasperReports
based on XMLDataSource, etc.
It is also important noticing that as we have shown, different
view technologies are not mutually exclusive so if you favor
XSLT , but you need to
generate a plain text document (like a JSON response for an AJAX
request), you can easily reuse the same business logic operations
and implement the view for just this request with a more
text-friendly template system like FreeMarker .
On the other hand, we might need such dynamic development for
the prototyping phase, but want to implement our final
business logic in, for example, compiled Java classes. No problem!
We just need to implement the Java classes that return the same XML
as our Groovy prototype
and then change the XML_SOURCE attributes starting with
"script://" to others starting with "class://".
As long as we return the same XML, nothing else needs to be
changed. Plain Java classes, session EJBs, Oracle's PLSQL procedures,
Hibernate -based DAOs--all are
different options for the XML_SOURCE origin, so there are plenty to
choose from.
Last, but not least, we used Groovy as the scripting
implementation language but, thanks to the scripting java.net project ,
we could have used other scripting languages by simply changing the
file the XML_SOURCE URL points to. It is worth mentioning that the
Scripting API allows you to choose the scripting engine to be
loaded depending on the extension of the file to be processed, so
it is just necessary to add the appropriate engine to the
classpath, and sometimes configure the scripting language
underneath, to be able to use other languages. Apart from testing
Groovy , at the time of
writing we had performed successfully the same tests
using other scripting engines and implementing the business logic
in Jython and PHP (Quercus) .
Implementation Notes
In the example, access to the database in the Groovy code is
done in an inefficient way, as it opens a database connection
for each request. But this is just a script for demonstration
purposes, and we didn't want to make it more complicated by
introducing a DataSource configuration, which depends on the
servlet container used.
If you are convinced that you want to go Groovy from beginning
to end, be aware that there are other options, like Grails , that might be more
suitable for you. The final goal of the technique explained in this
article was to show an architecture where you can swap Groovy with
other scripting languages; and later on replace them with
Java/PLSQL , etc., so
the approach is totally different as the requirements are also
different.
Resources
- Source code The sample code used in this document minus the
.jar libraries.
- WebLEAF : A servlet
framework whose main goal is to help in the development of web
server-side applications written in Java.
- HSQDLB : An SQL relational
database engine written in Java.
- scripting java.net
project : A project at java.net hosting script engines that
implement the JSR 223 Scripting APIs introduced with Java 6.
- Groovy : An agile
dynamic language for the Java platform with many features that are
inspired by languages like Python, Ruby, and Smalltalk, making
modern programming features available to Java developers with
an almost-zero learning curve.
- FreeMarker : A
"template engine;" a generic tool to generate text output (anything
from HTML to autogenerated source code) based on templates.
- XSLT : A language for
transforming XML documents into other XML documents.
- Jaxen : A Java XPath
engine under an Apache-style open source license.
- Scalable Vector
Graphics (SVG) : A language for describing two-dimensional
graphics and graphical applications in XML.
- Batik : A
Java-based toolkit for applications or applets that want to use
images in the SVG format for various purposes, such as display,
generation, or manipulation.
- JasperReports :
A powerful open source Java reporting tool that has the ability to
deliver rich content onto the screen, to the printer, or into PDF,
HTML, XLS, CSV, or XML files.
- Hibernate : A powerful,
high performance object/relational persistence and query
service.
- PLSQL : Oracle's
Procedural Language extension to SQL.
- Grails : An
open source web application framework that leverages the Groovy
language and complements Java web development.
- Jython : An implementation
of the high-level, dynamic, object-oriented language Python written
in 100 percent pure Java, and seamlessly integrated with the Java
platform.
- JRuby : A 100 percent
pure-Java implementation of the Ruby programming language.
- Quercus PHP : Caucho
Technology's 100 percent Java implementation of PHP 5, released under the
Open Source GPL license.
Daniel López currently works at the University of the Balearic Islands (UIB) as
technical leader of the web development group.