A resilient and dynamic development environment is being given unprecedented attention, and even the scripting language shows this feature, which is exactly what we need, that is, we always need to build an application that is maintainable and that meets our needs. If we want to get involved in scripting language, we should consider a new scripting API provided by Java SE 6: A language-independent framework that allows developers to use scripting languages in Java code. With this new set of APIs, we can use not only the features of scripting languages, but also many and Java Correlator tools.
In this article, we provide an example that will, as far as possible, embody the characteristics of this set of APIs. and using this API to implement a Web application based on model View Controller (MVC) mode, which also uses the groovy framework (a dynamic scripting language based on the Java platform, groovy is responsible for implementing business logic, Which is the M layer in MVC. and different techniques are used to implement the view layer in MVC. For the control layer (Controller) layer, we use webleaf, an open source framework for developing web programs based on the MVC pattern. To make this example easier to deploy and run, we used a java-based database: HSQLDB, a database that allows you to create a simple database with only two text files. In order to implement the view layer that generates an HTML user interface, we use the XSLT technology in this article, which is a language that converts XML documents into another form, and also uses Freemarker, a popular template engine for processing XML, As it can make XML as input data very easy.
In the example of this article we will first develop a Web page that will display different items of the database and allow us to select one so that we can view the details of an item. Although the program is simple, it is sufficient to articulate what this article is about.
First, the preparatory work
The first step is to build a sound basic standard Web application structure and add the latest Webleaf 3.x library, which can support the new Java Scripting API very well, and we can do this in five steps as follows:
1. First, create a directory called Test.
2. Then create a web-inf directory in the test directory.
3. Create two directories in the Web-inf directory: Lib and classes.
4. Put the Webleaf3.jar file in the Test\web-inf\lib.
5. A standardized Sevlet description document Web.xml is then established in Test\web-inf, which reads as follows:
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<!--
WebLEAF controller servlet configuration
-->
<servlet>
<servlet-name>WebLEAFController</servlet-name>
<servlet-class>org.leaf.LEAFManager</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- End controller servlet -->
</web-app>
This configuration file tells the servlet container to instantiate a Webleaf control servlet and start it when the context starts. Here we have to pay attention to a value of 2, and for a while we'll explain why this value is not 1, but 2.
Finally, we need to create a webleaf configuration file test.leaf.conf in Web-inf, which reads as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE WADSET SYSTEM "http://www.uib.es/leaf/LEAFwad.dtd">
<WADSET>
<WAD
NAME="Test"
INIT_MODE="INITS_ON_START_UP"
XML_ALLOW_SHOW="TRUE"
XSLT_NO_CACHE="TRUE"
>
</WAD>
</WADSET>
Although the configuration file above is a minimal configuration, it is sufficient for the example in this article.