Creating data access Web applications using Struts
A Struts-based Web application, also known as a Struts application, is a Web application that uses the Struts framework.
Using Struts to create a complex Web application can help make the application more maintainable. The application is easier to debug, easier to extend, and easier to understand. Using Struts tools can improve the efficiency by which you create the Struts-based application. The Struts tools work with the Web diagram editor.
Struts applications use the Struts framework to implement a model-view-controller design approach to building Web applications. As part of the controller portion of MVC, each Struts application has an action servlet configured in the application's deployment descriptor (web.xml) file. For a Web application to be a Struts application, the action servlet must be configured and the appropriate resources must be in the Web project. Some of these resources, which are created automatically when you create a Struts-enabled Web project with this product, are as follows:
An ApplicationResources.properties file in a subdirectory of the JavaSource directory and in a subdirectory of the WEB-INF directory
Struts JAR files in the lib directory in the WEB-INF directory
A Struts configuration file (struts-config.xml file in the WebContent\WEB-INF folder) and Struts tag libraries in the Web content directory
Additional entries in the .classpath file
Struts tools for application development
The Struts feature provides tools that help you use Struts to develop Web applications more effectively.
The Struts framework helps you create Web applications that are more maintainable. The Struts tools in this product help you create Struts-based Web applications more efficiently.
Benefits of Struts tools
The Struts feature provides the following benefits:
Lets you set up a Struts project automatically so that the Struts run-time classes (JAR files), tag libraries, and other Struts-related resources are located properly
Provides wizards to create form beans, Struts actions, and other Struts resources, thus simplifying the creation process and eliminating the need to update the configuration file manually
Includes a specialized editor that understands the syntax of the Struts configuration file
Validates the configuration, checking configuration files and JSP references to Struts objects (defined in the configuration file) when a JSP file is saved and built
Provides Struts support for editing, including the addition of Struts tags to the Palette view or the Web diagram editor palette
Renders Struts tags within Page Designer
Uses a Web diagram editor to design application flow, which has the following benefits:
Enables you to design a dynamic Web application visually
Efficiently documents an application and helps communicate its structure and flow
Provides quick access to resource-appropriate editors and wizards
Enables you to separate design from development
Helps you visualize existing applications
The Struts tools in the Struts feature provide the following editors:
Struts configuration file editor - Supports specialized editing of Struts configuration files.
Web diagram editor - Shows the overall structure of the Struts application, allows navigation to the editors for the underlying resources, and lets you edit some aspects of those resources directly.
Editors enhanced for Struts
In addition, the following editors have been enhanced:
Page Designer - Visually represents Struts tags, which you can drag and drop in the editor.
XML editor - Uses the configuration file DTD when you edit a Struts configuration file.
Struts configuration files
A Struts configuration file is an XML document that describes all or part of a Struts application. A Struts configuration file contains information about many types of Struts resources and configures their interaction.
A Struts configuration file contains the following categories of information:
To take effect, a Struts configuration file must be referenced from the Web deployment descriptor (web.xml file) for the configured Struts action servlet.
The Struts configuration file editor in the workbench is designed specifically to edit Struts configuration files. The XML editor has been enhanced to use the configuration file DTD.
If you create just one Struts configuration file, its default name is struts-config.xml.
Editing Struts resources from the Web diagram editor
In the Web diagram editor you can open editors on the underlying resources represented by the nodes on the free-form surface.
To start an editor for a particular resource, double-click on the node for that resource. For example, if you double-click on a node that represents an action mapping, the Struts configuration file editor opens and the correct action mapping is given focus.
To edit a connection resource, double-click the connection. If the connection represents a forward from an action mapping, the Struts configuration file editor opens and gives focus to the correct forward. If the connection represents a link from a Web page, one of two things may take place:
If the connection represents a single link between the Web page and the target, Struts opens the default editor for that file and places the cursor on the line on which the link is defined.
If the connection represents multiple links, a dialog pops up that lists all the links that the connection represents. When you select a link, the JSP editor opens and the cursor is placed on the line on which the selected link is defined.
If you double-click an unrealized node, the node is either automatically created, as is the case for an unrealized forward, or a wizard is invoked to create the node.
Realizing nodes in Web diagrams
When a node in a Web diagram is realized, the underlying resource represented by the node comes into existence.
To realize an unrealized node, either double-click the node (to invoke the appropriate wizard) or change its path to refer to an existing resource.
The following list describes the action that results from double-clicking:
The JSP wizard opens.
Note: Do not select a template from sample page templates, because this produces a JSP file with the Struts tags removed. Instead of samples, select a user-defined page template that you created. To create a page template, create a Struts JSP file and save it as page template from the menu Page Designer's File > Save As Page Template.
The HTML wizard opens. Through this wizard you can create the HTML page. The HTML page wizard automatically opens the JSP editor on the new HTML page, and the visual representation of the HTML page changes to represent a realized HTML page.
If the action mapping does not exist, the Action Mapping wizard opens. If the action mapping exists but is not configured, a configuration dialog box opens. The former case is the default. In the latter case the dialog box lists all the Struts configuration files that are defined for the Struts application (via the web.xml file). The Struts configuration file editor opens, and a new action mapping entry is generated and selected. You can change the default behavior by setting Web diagram preferences.
The Web Project wizard opens.
Java bean part
The New Java Class wizard opens.
If the form bean does not exist, by default the New ActionForm Class wizard opens. If the form bean exists but is not defined in a configuration file, a configuration dialog box opens, and the dialog box lists all the Struts configuration files that are defined in the web.xml file for the Struts application. The Struts configuration file editor is opened on the chosen configuration file, and a new form bean entry is generated and selected. You can change the default behavior by setting Web diagram preferences.
If the module does not exist, the Module wizard opens. If the module exists but the entry point action mapping does not exist, the Action Mapping wizard opens.
You can also realize an unrealized part by modifying its properties to identify it as an existing part. For example, an action mapping part can be made realized by changing its path to be the path of an existing action mapping. Changing the path of an action mapping or Web page part can be done by either selecting the part and changing the attribute on the properties page, or by double-clicking on the node's label, or by bringing up the pop-up menu of the part (right mouse clicking over the part) and selecting the Rename action. An unrealized connection that represents an action mapping forward can be realized in a way similar to the way described above. The difference being that the pop-up menu option is Edit the forward name.
Defining application flows with the Web diagram editor
To define the flow of your dynamic Web application, you can use the Web diagram editor, a visual editor for editing Web diagrams.
To define an application flow:
Create a Web diagram.
Edit the Web diagram.
Optionally, at any time set preferences for Web diagrams.