Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Apache Struts 2 in a Nutshell

Apache Struts 2 is a flexible control layer based on standard technologies like Java Filters, JavaBeans, ResourceBundles, Locales, and XML, as well as various OpenSymphony packages, like OGNL and XWork. The framework helps you create an extensible development environment for your application, based on industry standards and proven design patterns.

The framework provides its own web Controller component Struts is a Model View Controller framework. Struts provides Controller and View components, and integrates with other technologies to provide the Model and the View. For the Model, the framework can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like Cayenne, Hibernate, or iBATIS. For the View, the framework works well with JavaServer Pages, including JSTL and JSF, as well as FreeMarker or Velocity Templates, PDF, XSLT, and other presentation systems. The framework's Controller acts as a bridge between the application's Model and the web View.

To make it easier to present dynamic data, the framework includes its own library of markup tags. The tags interact with the framework's validation and internationalization features, to ensure that input is correct and output is localized. The tag library can be used with JSP, FreeMarker, or Velocity.

When a request is received, the Controller invokes an Action class. The Action class examines or updates the application's state by consulting the Model (or, preferably, an interface representing the Model). To transfer data between the Model and the View, properties can be placed on the Action class, or on a plain old JavaBean.

Most often, the Model is represented as a graph of JavaBean objects. Alternatively, the Model may be represented as a set of data-driven service methods. Preferably, the Model will The Model should do the "heavy lifting", and the Action will act as a "traffic cop" or adapter. The framework provides sophisticated, automatic type conversion to simplify transfering data between rich domain objects and text-only HTTP requests. To make it easier to access dynamic data obtained by an Action, the framework includes a library of custom tags. The tags interact with the framework's validation and internationalization features, to ensure that input is correct and output is localized. The tag library can be used with JSP, FreeMarker, or Velocity.

Struts Tags in a nutshell

Struts configuration in a nutshell

A web application uses a deployment descriptor to initialize resources like filters and listeners. The deployment descriptor is formatted as a XML document and named web.xml. Likewise, the framework uses a configuration file, named struts.xml, to initialize its own resources. These resources include action mappings, to direct input to server-side Action classes, and result types, to select output pages.

Here's a simple typical configuration (struts.xml) for a login workflow:

Code Block
<struts>
    <include file="struts-default.xml"/>

    <package name="default" namespace="/" extends="struts-default">

        <action name="Logon" class="mailreader2.Logon">
            <result name="input">/pages/Logon.jsp</result>
            <result name="cancel" type="redirect-action">Welcome</result>
            <result type="redirect-action">MainMenu</result>
            <result name="expired" type="chain">ChangePassword</result>
        </action>

        <action name="Logoff" class="mailreader2.Logoff">
            <result type="redirect-action">Welcome</result>
        </action>

    </package>
</struts>

Aside from actions and results, you can also specify exception handlers and interceptors. Interceptors specify the "request-processing lifecycle" for an action. (What happens to the request before and after the Action class fires.) You can specify both global and local lifecycles. If some of your actions respond to AJAX, SOAP, or JSF requests, you can simplify the lifecycle, and even just "pass through" the request, if you likeThe framework provides general-purpose defaults, so you can start using Struts 2 right away, "out of the box". As needed, you can override any of our defaults in your application's configuration.

Struts 2 is extensible. Very extensible. Every class deployed by the framework is based on an interface. We provide base classes, but you can substitute your own. In the case of Action classes, even the interface is optional. POJO web development is here!

The framework provides general-purpose defaults, so you can start using Struts 2 right away, "out of the box". As needed, you can override any of our defaults in your application's configuration. We provide the base framework, but you can still write your application your way.

...