This reference is under development. All Struts developers are requested to contribute and help in expanding this reference


Struts is the most popular framework for developing Java based web applications. Struts is being developed as an open source project started by Apache Software Foundation . Struts framework is based on Model View Controller (MVC) architecture.

Preface

edit

Action Framework in a Nutshell

edit

Within the MVC context the struts framework provides its own web Controller component 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 Hibernate , iBATIS , or Object Relational Bridge . For the View , the framework works well with JavaServer Pages , including JSTL and JSF , as well as Velocity Templates , XSLT , and other presentation systems.

The framework's Controller acts as a bridge between the application's Model and the web View. When a request is received, the Controller invokes an Action class. The Action class consults with the Model (or, preferably, a Facade representing your Model) to examine or update the application's state. The framework provides an ActionForm class to help transfer data between Model and View.

Most often, the Model is represented as a set of JavaBeans . Typically, developers will use the Commons BeanUtils to transfer data between ActionForms and the Model objects (or a Facade). Preferably, the Model will do the "heavy lifting", and the Action will act as a "traffic cop" or adapter.

Struts Config in a Nutshell

edit

A web application uses a deployment descriptor to initialize resources like servlets and taglibs . The deployment descriptor is formatted as a XML document and named "web.xml". Likewise, the framework uses a configuration file to initialize its own resources. These resources include ActionForms to collect input from users, ActionMappings to direct input to server-side Actions , and ActionForwards to select output pages.

The initialisation of struts is achieved through the inclusion of the following in the web.xml file

<servlet>
     <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
      </init-param>
     <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
     </init-param>
     <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
     </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>action</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

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

   <?xml version="1.0" encoding="ISO-8859-1" ?>
   <!DOCTYPE struts-config PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
         "http://struts.apache.org/dtds/struts-config_1_3.dtd">
   <struts-config>
       <form-beans>
           <form-bean
               name="logonForm"
               type="app.LogonForm"/>
       </form-beans>
       <action-mappings>
           <action
               path="/Welcome"
               forward="/pages/Welcome.jsp"/>
           <action
               path="/Logon"
               forward="/pages/Logon.jsp"/>
           <action
               path="/LogonSubmit"
               type="app.LogonAction"
               name="logonForm"
               scope="request"
               validate="true"
               input="/pages/Logon.jsp">
               <forward
                   name="success"
                   path="/pages/Welcome.jsp"/>
               <forward
                   name="failure"
                   path="/pages/Logon.jsp"/>
           </action>
           <action
               path="/Logoff"
               type="app.LogoffAction">
               <forward
                   name="success"
                   path="/pages/Logoff.jsp"/>
           </action>
       </action-mappings>
       <message-resources parameter="resources.application"/>
   </struts-config>

There are several other resources you can specify in the framework's configuration file. You can specify validations for the ActionForms in an XML descriptor, using the Struts Validator. A standard extension, Tiles , helps you build pages from smaller fragments.

Struts Action Framework is extensible. Every class deployed by the framework can be replaced by your own default class. The properties of your default class can be set using the Digester's set-property feature. This is one reason why there are so many contributor extensions.

Tiles is a contributor extension.

Features

edit

Screen definitions

edit

Create a screen by assembling Tiles : header, footer, menu, body, etc. Definitions can take place:

  • in a centralized xml file
  • directly in jsp pages
  • programatically in struts actions

Definitions provide an inheritance mechanism : a definition can extend another one, and override parameters.

Layouts

edit

Define common page layouts and reuse them across your web site. Define menu layouts, and use them by passing lists of items and links. Define a portal layout, use it by passing a list of Tiles (pages) to show. Reuse existing layouts, or define your own.

Dynamic page building

edit

Tiles can be gathered dynamically during page reload. It is possible to change any attribute: layout, list of Tiles in portal, list of menu items, etc.

Reuse of Tiles / Components
edit

If well defined, a Tile can be reused across multiple applications. Dynamic attributes are used to parameterize Tiles. It is possible to define a library of reusable Tiles. Build a page by assembling predefined components, giving them appropriate parameters.

Internationalization (i18n)

edit

It is possible to load different tiles according to Locale. A mechanism similar to Java properties files is used for definitions files: you can have one definition file per Locale. The appropriate definition is loaded according to the current Locale.

Multi-channels

edit

It is possible to load different Tiles according to a key. For example, the key could be user privilege, browser type, arbitrary name stored in session, etc. A mechanism similar to Java properties files is used for definitions files: you can have one definition file per key. The appropriate definition is loaded according to the key.

Prerequisites

edit

Apart from basic understanding of building a web application. The reader is required to have some sort of working knowledge in the following technologies.

Getting Started

edit

Struts framework provides the following benefits over conventional methods of Web Application Development.

  • Decoupling of business logic and presentation logic
  • Reusablity
  • Maintainability

These benefits are gained because Struts provides a system based on the "Model 2" paradigm of Java web application design, while also providing a framework for the application that is cleaner than a series of J2EE servlets.

Struts XML Tags and Attributes Reference

edit

(The following list is in the same order as the example above (of a simple configuration) and the example in link below of Struts tutorial on JBoss.)

  • <struts-config - outer tag that encompasses the other tags.
  • <data-sources -
  • <form-beans - defines a section of forms that encompasses one or more <form-bean tags.
  • <form-bean - defines a form :
    • name=
    • type=
  • <global-exceptions -
  • <global-forwards - defines a section of forward routes that are used, by default, in <action tags. It encompasses one or more <forward tags.
  • <action-mappings - defines a section of actions that encompasses one or more <action tags.
  • <action - defines an action, which can have one or more forward routes (with one forward attribute, or one or more <forward sub-tags) :
    • path=
    • forward=
    • type=
    • name=
    • scope=
    • validate=
    • input=
    • parameter= can be read from @struts.Action@ class by @mappings.getParameter()@
  • <forward - defines a forward route within an <action tag :
    • name=
    • path=
  • <controller -
  • <message-resources -
    • parameter=

Further Reading

edit