[ Team LiB ] Previous Section Next Section

Introduction to Struts Architecture

The Struts framework depends on the configuration and usage of both XML files and specific classes interacting with each other within a Web application. The main goal of Struts is to separate different aspects of design into logical blocks to free programmers and HTML/Web designers from actual Java code written in JSP files. Struts uses the struts-config.xml file, which is configured for each Web app and located in the /WEB-INF directory, to hold mappings and directives between pages. This file must have associated entries in /WEB-INF/web.xml to be used in the Web app. When application-specific Java code has to run, the struts-config.xml file directs the input received from the source file (an HTML, JSP, or servlet page, for example) to an action class. The action class then performs business logic and returns to the struts-config.xml file to get the mapping of the next page or object to call depending on what happened in the action class. This handles the display of errors and different pages based on input, or logic within the action class itself.

Forms from input files (most likely HTML or JSP) can be configured to automatically validate themselves via ActionForms. Using Struts custom tag libraries, JSP pages are very clean and free of any Java code. Even error-handling tags are configured to display errors at the page or form level. Figure 19.1 presents a graphical layout of the Struts application flow. Starting with a JSP file, the form is submitted. The action mappings contained within the struts-config.xml file are then used to call the ActionForm class to verify the input. If there's an error, the page is recalled and all the Struts error tags display the proper error. If verification is successful, the action mapping designates where to go next. The most common answer is to an action class to process business logic. An action class processes the logic and input from the form and then requests back to the mapping to find the next page to call or refers to the calling page with errors. Also represented on the diagram are Struts-configurable text parameters used with the <bean:message> tag. This enables custom text to be replaced easily, and allows for internationalization, which is the capability of Java applications to use other languages and formats in addition to the one the application was written in.

Figure 19.1. Basic Struts application flow.

graphics/19fig01.gif

    [ Team LiB ] Previous Section Next Section