Web Development in J2EE Using the MVC Design Pattern
Web services application development is a huge topic covering a broad range of technologies and topics. In this article we will discuss Web services development in J2EE and examine the Model/View/Controller design pattern, thereby explaining how it separates business logic implementation and persistence from presentation. We shall briefly introduce the Struts Framework, one that facilitates web services development within the IBM WebSphere Studio Application Developer edition. (This article was originally published in the June 2004 issue of Plug-In).
Web Development in J2EE Using the MVC Design Pattern - Applying the Mediator Design Pattern (Page 5 of 5 )
WebSphere architects have taken the MVC concept one step further by applying the Mediator design pattern introduced in Design Patterns by Erich Gamma. The Mediator/Controller pattern places another layer of indirection between the view and the model/domain. The Mediator/Controller captures and decouples application-specific functionality from presentation technology by implementing behavior usually existing as methods in presentation classes.
The mediator can be better understood by viewing it as an abstract interface that must be implemented by providing a concrete mediator object. When a client submits a request, the mediator intercepts the request and delegates the task to a concrete class of objects to fulfill mediator/controller directed tasks. The mediator is also responsible for controlling and coordinating the interactions of groups of objects. As Gamma explains, “the Mediator serves as an intermediary that keeps objects in the group from referring to each other explicitly. The objects only know the mediator, thereby reducing the number of interconnections.” Doing so places behavior in one class, thus allowing the behavior to be modified or replaced locally. Abstract interface substitutability is the key to object reuse.
Introducing the Front Controller Pattern
The J2EE specification contains the “Front Controller Pattern,” a design pattern that defines how a Web tier controller should be implemented. It is located at “http://java.sun.com/blueprints/patterns/index.html.” It reads as follows: “The Front Controller pattern defines a single component that is responsible for processing application requests. A front controller centralizes functions such as view selection, security, and templating, and applies them consistently across all pages or views. Consequently, when the behavior of these functions needs to change, only a small part of the application needs to be modified.” This is precisely the methodology the Jakarta Struts framework uses. The framework is comprised of several classes/components. In addition, the framework organizes design patterns and provides a generic approach to designing Web applications.
The Struts framework was created by Craig R. McClanahan in 2000. As previously demonstrated in the illustration, the Struts framework resides within the Web container component. This allows the Struts application to leverage the advantages of services provided by the container, i.e. managing requests via the HTTP and HTTPS protocols.
The Struts Framework provides a modular framework from which you can develop your Web application. It is comprised of a controller servlet, numerous beans and classes, configuration files, and tag libraries. Let’s review some of the components the Framework supports:
Struts Tag Libraries
Bean Tag Library
HTML Tag Library
Logic Tag Library
Template Tag Library
The Bean Tag library encapsulates tags needed to display static and dynamic Web pages. The tags are used to store and display data, write international type messages, and embed scriptlets for JSPs. Here is a list of tags included in the Bean Tag library:
Define - Define new attributes and make them available at a pre-defined scope, i.e. request, page, session, or application scope.
Include - Include pages and fragments from other files in the current file’s source.
Internationalize requested messages from the Resource file and replacing the current tag with an appropriately internationalized message in any language.
Write - Write data from exposed beans at any level of scope.
The HTML Tag library includes tags for all standard HTML controls. This library provides two important features: linking and internationalization. The tags are used to create page links in two formats: hyperlink or anchor.
The Logic Tag library is directly responsible for basic first and second order logic functions utilized to portray JSP flow and functionality as well as the controls they provide, depending on the state of the form beans and input beans.
The Template Tag library offers support for creating dynamic templates based on the JSP Framework for all pages sharing a common format. These are similar to style sheets or the standard include directive in JSP technology. They are dynamic. Here are three template tags:
Get - Retrieve content from a request scope bean.
Insert - Insert the retrieved content into the specified template.
Put - Create a request scope bean that specifies content used by the get tag.
In order to bring all of these components together, we use a set of XML-configuration files to store mappings of different actions.
Enterprise Java Programming with IBM WebSphere, Kyle Brown et al, IBM Press, 2003
Building J2EE Applications with IBM WebSphere, Nillson and Mauget, Wiley, 2003
Programming Jakarta Struts, Cavaness, O’Reilly, 2003
Enterprise Java Programming with IBM WebSphere, Kyle Brown et al, Addison-Wesley, 2001
Interoperability Between .NET & J2EE, Peltzer, McGraw-Hill/Osborne, 2004
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.