Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Jump the hurdles of Struts development

Jump the hurdles of Struts development

Tutorial Details:

Jump the hurdles of Struts development
Jump the hurdles of Struts development
By: By Michael Coen and Amarnath Nanduri
Tips for solving common difficulties in Struts
nless you have been living under a rock or in a cave for the past few years, you have most likely heard of the Struts framework . Struts is the open source initiative sponsored by the Apache Software Foundation and was created to encourage the Model-View-Controller (MVC) design paradigm within a Web application's presentation layer. Struts implements the MVC pattern using the Service to Worker pattern. A well-designed architecture strives to be loosely coupled and highly cohesive. Struts provides the mechanism to achieve this design goal in the presentation layer of multitiered, enterprise Web applications.
One of the most daunting tasks facing enterprise application architects is the presentation layer's creation and maintenance. Users expect highly functional, robust, and elegant graphical user interfaces. Thus, the presentation layer's codebase winds up greatly outnumbering that of the application layer. In addition, the advent of different display platforms such as wireless phones and PDAs has made an already complex situation much more complex.
Various books and articles already cover Struts's inner workings and teach how to use the framework. This article elaborates on the issues facing Web application developers who use Struts and how to resolve them. Many of the following approaches can be abstracted and applied to different MVC frameworks such as the upcoming JavaServer Faces specification. Craig R. McClanahan, one of the original creators of Struts, leads that specification.
This discussion's topics cover those areas that present the most hurdles while building a J2EE (Java 2 Platform, Enterprise Edition) application using Struts with BEA WebLogic Server. We cover the following specific issues:
Creating/maintaining struts-config.xml
Form/session management
Relationships of Struts mappings and the user interface
Managing the Back button
User authentication
User interface control flow
Exception handling
Testing
Take two and call us in the morning
The Struts framework unarguably eases the development and maintenance of user interfaces for enterprise applications. However, after working with Struts on even a simple application, one quickly realizes the nightmare that is struts-config.xml . That file can quickly become unwieldy at best. When building an enterprise application, struts-config.xml can grow in excess of 500 action mappings, making it virtually unmanageable.
We recommend two tools to help manage this headache. First, document your user interface flow using Microsoft Visio and StrutsGUI from Alien-Factory. StrutsGUI is a Visio stencil that helps depict user interface flow diagrams using Struts nomenclature. There is a hidden gem within the Struts stencil item: by right-clicking this option, selecting Edit Title Properties, and then selecting the Tools option, you can generate a struts-config.xml file based on this diagram. For example, the simple application shown in Figure 1 generates the following struts-config.xml shown in the code below:
Figure 1. StrutsGUI model. Click on thumbnail to view full-size image.








"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">









type="com.agilquest.onboard.presentation.actions.LoginAction">
Authenticates and authorizes a user.



To make your user interface flow diagrams more complete, we recommend an additional step when using StrutsGUI. Within your StrutsGUI Visio document, you can easily link each JSP (JavaServer Pages) page to its actual screen shot in the application. Not only does creating this artifact aid in documenting the application, but more importantly, it becomes an excellent tool for training new developers about the user interface design.
Another tool that helps manage Struts applications is the Struts Console created by James Holmes. In essence, it provides facilities that enable you to get to the same endpoint as StrutsGUI, but differs in approach and strengths. Both tools perform well, and either will enhance a Struts-based enterprise application's maintenance.
Now, where did I put that form?
ActionForm session management can be tricky. What should the ActionForm 's life be? Should it be in request scope or session scope? One solution to this problem puts the ActionForm in session for the life of the functionality it is supposed to represent.
In such a case, how do you maintain these ActionForm objects generically? Who assumes the responsibility of cleaning up these ActionForm objects once they are no longer needed? A typical scenario is when a user traverses from one functionality to another through a menu. In that case, the old ActionForm objects should be removed from the session and new ActionForm objects created. A centralized Action class, called MenuAction , which deals only with the menu traversal, should be present. This Action class deletes the redundant ActionForm objects from the session. It then forwards the user to the new page where new ActionForm objects are created.
Given this, how do we show the different menu items to the user based on her role or permissions? This menu should also be internationalized, and it can change based on the user permissions; that is, if the permissions change, the menu must change accordingly. One such approach persists the user's permissions. When she logs in, a MenuFactory creates menus from these permissions. For additional safety, the MenuAction class can then authorize the user before permitting her to proceed to the selected functionality.
A rule of thumb in naming the ActionForm objects in struts-config.xml is to end the object name with Form, thereby simplifying the maintenance of these forms in session. For example: ReservationForm , SearchUserForm , BankAccountForm , UserProfileForm , and so on.
The code below further clarifies ActionForm (s) management by illustrating a generic menu traversal action with the Action mappings:
public class MenuAction {
public ActionForward perform(ActionMapping _mapping,
ActionForm _form,
HttpServletRequest _request,
HttpServletResponse _response)
throws IOException, ServletException {
// Check end-user permissions whether allowed into the requested
// functionality
checkIfUserAllowedToProceed(_mapping, _form, _request, _response);
// Clean up the session object (this logic is in its own method)
String formName = null;
HttpSession session = _request.getSession();
Enumeration e = session.getAttributeNames();
while(e.hasMoreElements()) {
formName = (String)e.nextElement();
if (formName.endsWith("Form")){
session.removeAttribute(formName);
}
}
// Now find out which functionality the end-user wants to go to
String forwardStr = _request.getParameter("nextFunctionality");
if (forwardStr != null && forwardStr.trim().length() > 0){
return _mapping.findForward(forwardStr);
}
else {
return _mapping.findForward("index");
}
}
}
The following Action mapping is an example of how to implement an action based on a menu selection:

type="x.y.z.MenuAction"
input="/menu.jsp">





The example and the mapping are self-explanatory.
Tell me again, how are we related?
Any JSP can have one to many entry points and one to many exit points, depending on the complexity of the page itself. Recognizing these relationships is paramount to understanding and maintaining the complexity of the user interface. We have defined the relationships between a JSP page and an Action class as:
1:1 relationship
1:N relationship
N:N relationship
1:1 relationship
In a 1:1 relationship, a user goes from one JSP page to another through an Action class; this facilitates tight coupling between a JSP page and an Action . The only extra overhead is one Action mapping in struts-config.xml . This single Action with only one Action mapping in struts-config.xml can be used to go from one page to another.
Referencing one JSP page directly through another is poor practice; the user's permissions that go to the target JSP page cannot be checked (if applicable). This also leads to maintenance issues. To avoid these issues, always go from one JSP page to another through an Action class:

type="x.y.z.One2OneAction"
input="/test1.jsp">


1:N relationship
A slightly more complex relationship is one where a JSP page has multiple exit points but a single point of entry, referred to as a 1:N r


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Jump the hurdles of Struts development

View Tutorial:
Jump the hurdles of Struts development

Related Tutorials:

Boost Struts with
Boost Struts with XSLT and XML
 
Mix protocols transparently in Struts
Mix protocols transparently in Struts
 
Take command of your software
Take command of your software
 
Rumble in the jungle: J2EE versus .Net, Part 1
Rumble in the jungle: J2EE versus .Net, Part 1
 
A first look at JavaServer Faces, Part I
A first look at JavaServer Faces, Part Learn how to implement Web-based user interfaces with JSF
 
Jump the hurdles of Struts development
Jump the hurdles of Struts development
 
Excellent tutorial on Struts and Tiles
Excellent tutorial on Struts and Tiles This tutorial assumes knowledge of Java, JDBC, Servlets, J2EE (with regards to Web applications) and JSP Struts in a holistic manner, minus the beads and crystals. The Tiles framework makes creating reusable pages
 
Strut your stuff with JSP tags
Learn how to use the custom tags from the open source Struts library and create extensions that ease the coding of properties associated with field values and user input validation. The Struts package is part of the open source Jakarta project.
 
Welcome to the Apache Struts Tutorial
This is the complete Struts Tutorial. Explains ActionForm Action Class Validation Framework.
 
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together.
 
Developing Simple Struts Tiles Application
In this tutorial I will show you how to develop simple Struts Tiles Application. You will learn how to setup the Struts Tiles and create example page with it.
 
Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three.
 
Struts and Tiles aid component-based development
In the Java world, Struts is one of the best-known and most talked about open source embodiments of MVC.
 
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java Open Source Web Frameworks in Java Struts Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open
 
Jakarta Struts Interview Questions
Jakarta Struts Interview Questions Jakarta Struts Interview Questions Q: What is Jakarta Struts Framework? A: Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications.
 
Beginner to advance guide to the Apache Struts
Beginner to advance guide to the Apache Struts The Complete Apache Struts Tutorial This complete reference of Jakarta Struts shows you how to develop Struts applications using ant and deploy on the JBoss Application Server. Ant script is provided
 
Struts Guide
Struts Guide Struts Guide This tutorial is extensive guide to the Struts Framework. In this tutorial you will learn how to develop robust application using Jakarta Struts Framework. This tutorial assumes that the reader is familiar with the web
 
Struts HTML Tags
Struts HTML Tags Struts HTML Tags Struts provides HTML tag library for easy creation of user interfaces. In this lesson I will show you what all Struts HTML Tags are available to the JSP for the development of user interfaces. To use the Struts
 
Developing Simple Struts Tiles Application
Developing Simple Struts Tiles Application Developing Simple Struts Tiles Application Introduction In this section I will show you how to develop simple Struts Tiles Application. You will learn how to setup the Struts Tiles and create example
 
Understanding Struts Controller
Understanding Struts Controller Understanding Struts Controller In this section I will describe you the Controller part of the Struts Framework. I will show you how to configure the struts-config.xml file to map the request to some destination
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.