Web frame design and implementation (1): MAVERICK
Middle and Open Source Studios in JStanghz@163.com September 2003
The reasonable use of the Web framework in the J2EE project project can effectively improve productivity, enhanced maintainable and scalability. At present, it is currently based on the MVC mode Struts structure, but in the treasure house of open source projects, in addition to Struts, there are many other unique, excellent framework structures, such as MAVERICKWORK Spring Tapestry Turbine, etc., there is a reason for their own existence And value, only understand the design ideas and performance characteristics of other frameworks, in order to flexibly choose a more suitable design framework in project practice, this paper mainly introduces the design and implementation of the MAVERICK framework.
The MVC mode may be the most classic design pattern in software design. The model view controller of the Smaltalk class library touches the inspiration, clear hierarchy of software design, clear responsibilities, smooth workflow to make software design Added an artistic beauty. The MAVERICK framework is hidden in this beauty, and the name is implied in the name of MVC). The MAVERICK framework is committed to implementing the MVC mode, and its 1.0 version is released early in 2001. Jeff Schnitzer, one of the two major developers is also the creator of the open source community's famous test tools, the current latest version is 2.2. Quickly start to explore the basic theory and design ideas of MAVERICK, let us quickly experience the MAVERICK's function and charm through the configuration of the Friendbook-JSP instance. 1. Download the Maverick Basic Frame and Document. (Reference information) MAVERICK is divided into basic framework and extension framework, basic framework includes in maverick-2.2.0.zip, extended framework includes Opt-Domity, Opt-Betwixt, Opt-Fop, Opt-Velocity, OPT- Perl, Opt-Struts can choose to download as needed. 2, unzip the maverick-2.2.0.zip file, run Ant in the MAVERICK's Examples directory, generate the Friendbook-Jsp.war file under the build folder. 3, if we use Tomcat 4.0 to debug, copy the friendbook-jsp.war file to the Tomcat-4.0 / webapps directory, copy the log4j.jar file under the MAVERICK / LIB directory to Tomcat-4.0 / lib. If your system's JDK version is below 1.4, you must copy the XML-Apis.jar file under the MAVERICK / LIB directory to Tomcat-4.0 / lib. 4. Start Tomcat Type: http: // localhost: 8080 / friendsbook-jsp, you can see the following interface:
According to the operation of the demo process, open the source code, refer to the technical introduction of this article, you can start your MAVERICK exploration tour. Functional Features: MAVERICK claims to integrate Struts, WebWord, Cooco, is a simple and flexible, completely implemented an abstract framework of MVC mode, allowing you to use different templates and conversion technology to indicate layer logic. The specific characteristics are 5, simple practical, easy to understand. Following a simple design is the principle of achieving it, providing you most needed. Its core layer is simple, powerful, strong extensibility. 6. Adopt the design idea of inserted expansion integration, the core workflow is simple and clear, can understand the essence of the framework in the shortest time. The expansion module has a wide range of functions, and it can be used flexibly. 7, completely independent representation layer design. Select JSP (based on JSTL standard) Velocity Domify / XSLT according to preferences. 8, configurable conversion pipes. The transparent conversion of JavaBean data to XML, including support for XSLT, DVSL, FOP, Perl., Etc. 9, based on standard XML configuration. 10, simultaneously support the Struts type independent controller and the "Throwaway" controller of the WebWork type. 11, realization of multi-platform. Scalable to .NET and PHP. Frame structure and process analysis: The MAVERICK framework is a standard MVC model design with a clear hierarchy and responsibilities. The following is a simplified map:
Its basic workflow is the same as follows: 1, and the Struts framework, MAVERICK uses a unified controller servlet as an entry point, implemented by org.infohazard.maverick.dontroller class, defined in the web.xml file, all URLs are mapped Go to the Controller class. When an HTTP request is sent to the Controller class, the Controller gets the configuration from /Web-inf/maverickl.xml, generates an instance of org.infohazard.maverick.flow.Controller object, which typically inherits the throwawaybean2 super class. It should be described herein that MAVERICK is different from struts, and the request controller is usually the benefit of the JavaBean component, does not distinguish the controller (Actionform), and each new controller does not have to be a thread safe, unnecessary Support concurrent, but also results in the value of the controller instance. 2. The component properties of the application controller are set from the request parameters by reflecting, and the Apache Commons Beanutil package is filled. 3. Call the PERFORM () method of the application controller, set the model object displayed by the ControllerContext object during the execution process, and return the view name defined in MAVERICK.XML after calling the business object. 4. Place the model object set by the setmodle () method in the request attribute of the servlet with the keyword "modle". 5, perform JSP or Valocity view template. Or generate XML with a model object, convert XML into XSLT through the XSL file defined in MAVERICK.XML and return to the client. The above workflow only describes the application controller configuration based on the ThrowawayBean2 hyperclass, and MAVERICK provides extensions of different application controller types: ThrowawayBean2, FormBeanuser, throwayformbeanuser, ControllerwithParams, which makes MAVERICK workflows can be customized by customers, implementation The height configurability of the framework. Web.xml configuration file Configuration Web.xml is the first step in developing MAVERICK, all commands (Commons) are mapped in the "* .m" extension to the Controller Servlet.
servlet>
servlet-maping>
web-app>
It can be seen from the above that the Maverick.xml file is very similar to Struts. MAVERICK.XML configuration file MAVERICK profile compared to Struts is easy to understand, it does not have a cumbersome DTD to trouble you, everything is simple, and a glimpse. As shown below: (Selected from the MAVERICK Download Pack Friendbook-JSP Instance) XML Version = "1.0"?>
$ ID: MAVERICK.XML, V 1.6 2003/01/12 04:03:22 Lhoriman EXP $
$ Source: /cvsroot/mav/maverick/examples/friendbook-jsp/web-inf/maverick.xml ,v $
->
view>
views>
view>
commman>
view>
commman>
......
commands>
Maverick>
Three basic concepts can be found in the above configuration files: Command Controller View.
Command
view>
views>
View LoginRequired can be accessed by other View elements defined in Command. Its ID attribute is to be specified. It can be associated with the Name and REF attributes of other View elements. 2, as a child element of Command. It defines a partial view associated with Command, which can be associated with the id attribute of the global view through the REF property, and access the global view. Such as
commman>
{
Public static final string default_dest = "friends.m";
Protected string name;
Public string getname () {return this.name;
Public void setname (string value) {this.name = value;
Protected string password;
Public String getPassword () {return this.password;}
Public void setpassword (string value) {this.password = value;}
Protected string dest;
PUBLIC STRING GETDEST () {Return this.dest;}
Public void setDest (string value) {this.deest = value;}
protected string perform () throws exception
{
If (! this.login (form.getname (), form.getpassword (), ctx))
{
Return Error;
}
Else
{
IF (this.Dest == null || this.dest.trim (). Length () == 0)
THIS.GETCTX (). setmodel (default_dest);
Else
THIS.GETCTX (). SETMODEL (this.Dest);
RETURN SUCCESS;
}
}
}
The Loginsubmit class defines three protected properties with the GET () and set () methods: Name, Password, DEST, rewritten the Perform () method inherited from the ThrowawayBean2, and call the setmodel () method of the ControllerContext class creates view model. And return view name. The Loginsubmit class integrates component properties and control logic, and placed in the request attribute of the servlet with keyword "modle". In the JSP file representing the layer exposes its property value with modle.xxx expression. Such as
" name = "password"> td>
td>
The FormBeanuser: The FormBeanuser class inherits the Controllersingleton interface, which is a separate controller class, which references the FORMBEANL class defined outside instead of defining the component attribute in itself, it must be linear security. If you want to save the model (MODLE) in the session or don't like the zero-chaos of throwaWaryBean2, this controller process can be used, which is very similar to the Action of the Struts structure. The Loginsubmit class in the above example can be rewritten as follows:
Public class loginsubmit2 Extends FormBeanuser
{
Public static final string default_dest = "friends.m";
Protected Object Makeformbean (ControllerContext CCTX)
{
Return new form ();
}
Protected String Perform (Object Formbean, ControllerContext CTX) THROWS EXCEPTION
{
Form form = (form) formbean;
If (! this.login (form.getname (), form.getpassword (), ctx))
{
Return Error;
}
Else // They area now Logged in ...
{
// Target of Redirect
IF (form.getdest () == null || Form.getDest (). Trim (). Length () == 0)
CTX.SETMODEL (Default_Dest);
Else
CTX.SETMODEL (Form.getDest ());
RETURN SUCCESS;
}
}
}
The form definition of the form is as follows:
Public Class Form
{
Protected string name;
Public string getname () {return this.name;
Public void setname (string value) {this.name = value;
Protected string password;
Public String getPassword () {return this.password;}
Public void setpassword (string value) {this.password = value;}
Protected string dest;
PUBLIC STRING GETDEST () {Return this.dest;}
Public void setDest (string value) {this.deest = value;}
}
Call as follows in the JSP file:
" "" "Name =" Password ">
ThrowawayFormBeanuser: This class is a mixed bonus of the ThrowawayBean2 and FormBeanuser classes, which follows the ThrowawayBean2 process and allows you to call the outside FORMBEAN class.
ControllerWithParams: Control class with parameter access. Such as:
controller>
Through these four controller types, the MAVERICK framework provides flexible flow control, most commonly used to extend the control process of the ThrowawayBean2 class, but readers who are familiar with the Struts framework may be more intimate to implement the FormBeanuser extension. The MAVERICK download package contains both the Friendbook-JSP and Friendbook-JSP-FBUs that extends through ThrowawayBean2 and FormBeanusers, and readers can carefully experience according to source code. The view indicates that the Struts binding is different from the JSP view, and the MAVERICK supports multi-view representation to avoid tight coupling of the control layer and the representation layer, making the representation layer more clearly and clear. Maverick supports view templates such as JSP Velocity XSLT. JSP: JSP is the most wide view template, which has powerful representation and control functions, and you have a key role in the design framework of Modle1. However, in the MAVERICK framework it is limited to the scope of the reference represented by the view. MAVERICK supports JSP standard label jstl, does not provide its own special label library, reducing learning difficulty, avoiding framework depends on proprietary knowledge. It is very simple to implement JSP in the MAVERICK, and set the following in the maverick.xml configuration file:
view>
views>
The Default-View-Type property of the MAVERICK element is set to "Document" to access the JSP document, and the JSP is derived by the MODLE keyword references the properties and methods of the Modle keyword in the servlet request attribute. Velocity: Velocity is an open source project of Jakarta Apach, which is a pure template engine that does not depend on the Servlet API, which can be used in additional applications other than web programs and can be tested outside the servlet container. Velocity's core is Velocity Template Language (VTL), which is similar to the JSTL of the template language. It is easy to learn and understand. Velocity is a simple and high-performance template language that can effectively expose the properties and methods of the controller, and implement thorough separation of the controller logic and view template. Implementing Velocity in Maverick is also very simple, first registering velocityViewServlets in the web.xml configuration file, such as:
servlet>
servlet-maping>