|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
General Java Web Application Framework
Web Application Framework
By: Craig McClanahan
Mar. 1, 2001 12:00 AM
The J2EE architecture is a great advance for developers. Its standardized framework defines and supports a multitiered programming model, freeing application developers to concentrate on solutions. This article is based on version 0.5 of Struts; some of the APIs will change slightly in subsequent versions. There are a number of ways to architect applications using J2EE technologies, including JSPs, servlets, and EJBs. However, there aren't any application development frameworks available that address how developers should create applications based on the J2EE architecture. The development community has recognized the need for an open-source framework that's useful in building Web applications using J2EE technology. This has led to the creation of the Struts project, a burgeoning grassroots movement that will soon become a J2EE "household name."
Struts Introduction
The Struts charter is to provide an open-source framework that facilitates building applications based on the J2EE servlet and JSP technologies. The primary functionality provided by Struts includes request dispatching, tag lib- raries to accelerate GUI development, internationalization, and automatic form population. Struts encourages application architectures based on the Model-View-Controller (MVC) design paradigm, also known as Model 2.
Model 1 (page-centric) vs Model 2 However, while Model 1 is well suited to relatively simple applications, it's limited for complex ones. For example, the fact that the JSP pages in a Model 1 design contain both the presentation and control logic means the application is more difficult to maintain. A large amount of Java code within the HTML eliminates the role separation between Web page designer and Java coder, creating dependencies across multiple developers and increasing the likelihood of errors. In addition, Web applications inherently have flow control issues, which can lead to many problems unless proper precautions are taken to avoid them. In many cases, "proper precautions" mean the creation of a great deal of duplicate code. For instance, to facilitate proper security, a developer must not make assumptions about the users' authentication before they arrive at the current page. This requires some sort of user authentication to happen in every page. In a Model 2 design the application flow is controlled by a central servlet that utilizes an implementation of the Mediator design pattern to delegate requests to the appropriate handler. The request handler will most often be a presentation JSP; however, it could also delegate a processing page JSP or servlet to handle the application logic. Then the JSP or servlet forwards the request to the appropriate presentation page back through the servlet. To acquire the appropriate page to forward the request to, resolve the logical name that's passed from a configuration file or database to the servlet as part of the pathinfo or as a request parameter. This provides a loose coupling of the pages and components within the application.
Model-View-Controller
The Struts framework provides developers with a well-thought-out design that allows you to focus your program flow, validation, and request dispatching. Struts accomplishes this by providing specific components for each piece of the MVC architecture - Model, View, and Controller.
Model
The ActionForm class serves several purposes in Struts. Typically, when building a Struts application you'll implement a class that implements the ValidatingActionForm interface for each input form in your application. The ActionForm should contain the attri- butes that you've defined in your input form using the tag libraries provided by Struts. When the form is submitted, ActionServlet creates or reuses an instance of the ActionForm associated with the input form and adds this instance to the user's session. The attributes of the input form will be populated in the ActionForm instance by calling the corresponding mutator methods. You're required to implement accessor and mutator methods for each property defined in your ActionForm.
Once the ActionForm is populated, the validate method is invoked. The validate method is responsible for ensuring that the user populates the required fields with data in the appropriate format. As you can see from the ValidatingActionForm interface, the validate method returns an array of strings. If the validate method determines there's invalid data in the ActionForm (nonexistent or otherwise incorrect), the strings returned from the validate method are communicated to the user (through the Once the validate method returns null, indicating there are no errors, the ActionServlet calls the corresponding perform method of the Action class. The perform method is responsible for processing the request and subsequently forwarding the request to the appropriate view component (a JSP) as indicated by the returned ActionForward in- stance. When implementing an Action, you can either implement the Action interface or extend the abstract base class ActionBase. ActionBase provides default implementations for the getLocale, setLocale, getResources, isCancelled, and saveErrors methods. It's not recommended that the Action contains business logic, but it should make calls into the business component stack that implements the logic. After the Action has processed the request, it should return an ActionForward object that identifies the JSP page to which control should be transferred. This JSP is responsible for generating the appropriate response to the requesting device. The benefit Action provides is that business components don't need to be aware that they're accessed from a servlet/Web environment. Business objects shouldn't take ServletRequest and ServletResponse as parameters. This tightly couples business components to a Web environment and limits the scope in which they can be used.
View
<input type="text" name="userName" value="<%= user.getUserName() %>">Using Struts custom tag libraries, the above would become: <struts:text name="userName">
The advantage here is that HTML developers may not be familiar with scripting beans, such as the one shown in the first snippet. The Struts tag library gets around this by providing a simple tag library that can be used by nonprogrammers. The second snippet assumes the
When implementing a Struts application, use the Struts tag libraries to construct the user interface. Included in this tag library are tags for typical form elements such as checkboxes, hidden fields, and radio controls. In addition, the library contains various tags for displaying data, creating hyperlinks, and internationalization.
Controller
You'll need to implement an ActionMapping class if additional mapping information is specified in the action.xml file. However, if no such additional attributes are needed, Struts provides a default implementation (ActionMapping) that knows how to deal with the default XML format of action.xml.
In the sample application that's part of the Struts distribution, there are two additional attributes in action.xml - success and failure. These attributes define which JSP page should be forwarded to which request based on a success and failure condition of the validate method of the ActionForm class.
A Note on Internationalization (I18N)
Beyond what's already provided by Java, Struts provides a MessageResources class that lets you treat a set of ResourceBundles as a database. In Java, when you request a message from a ResourceBundle, it's based on the default locale of the JVM. This is not useful when the JVM in which your application is running is serving up content for individuals with different preferences. Struts allows you to request a message string for a particular user for a specific language preference. This is implemented by having the application store a locale object within the user's session under the name "org.apache.struts.
action.LOCALE". If no locale object is found in the user's session under this key, the default resource bundle will be used.
To support multiple languages in your application using Struts, provide a resource bundle for each locale that you want to support, along with the appropriate extension. The base name of the resource bundle to be used by the application is specified in the application's deployment descriptor.
The message tag is used as follows within an HTML/JSP form:
The Future of Struts
There are a number of ways to participate in the Struts project. You can join one of the Struts mailing lists by sending e-mail to the user (struts-user-subscribe@jakarta.apache.org) or developer (struts-dev-subscribe@jakarta.
apache.org) mailing lists. The user mailing list is made up of users of the Struts framework, while the dev mailing list is made up of developers interested in furthering the Struts framework.
You can contribute to Struts by providing patches and modules to the dev mailing list. There's also a "Todo" list on the Struts Web site, http://jakarta.apache.org/struts/, that outlines the immediate tasks the group is working on. Help is always welcomed.
Last, you can achieve committer status, which enables you to have direct access to the source control. To become a committer you must be voted in; the rules can be found on the Jakarta Web site, http://jakarta.apache.org/guidelines/index.html.
References and Resources
Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||