Creating Struts actions

An action is an instance of an Action subclass that implements a portion of a Web application and returns a forward. An action mapping is a configuration file entry that, in general, associates an action name with an action.

An action can perform tasks such as validating a user name and password or performing a computation. For an example of validating a user name and password, see /login in the

struts-config.xml Struts configuration file in the

struts-example sample application.

An action mapping is a configuration file entry that usually contains a reference to an

Action class. This entry can contain a reference to a form bean that the action can use, and can also define local forwards and exceptions that are visible only to this action.

To create a Struts application, configure an action servlet in the deployment descriptor of the application. An action servlet is a servlet that is started by the servlet container of a Web server to process a request that invokes an action. The servlet receives a forward from the action and asks the servlet container to pass the request to the forward URL. An action servlet must be an instance of an

org.apache.struts.action.ActionServlet class or of a subclass of that class. An action servlet is the primary component of the controller. A default servlet is automatically added by the Struts tools when you add Struts support to a dynamic Web project.

A Struts action does not always require a form bean reference, depending on how the action is used in JSP files.

To create an action and optionally create an action mapping:

  1. In the Enterprise Explorer of the Web perspective, right-click the name of a project.

  2. On the pop-up menu, click

    New | Other | Web | Struts | Action Class | Next. The New Action Class wizard starts.

  3. On the New Action Class page:

    1. In the

      Java package field, specify the name of the Java™ package in which the class is to be created.

    2. In the

      Action class name field, type the name of the new action class.

    3. If you want to specify modifiers that apply to the new class, select one or more of the following check boxes:

      public,

      abstract,

      final.

    4. Verify the value in the

      Superclass field.

    5. For each interface that you want to create, click

      Add and type the name of the interface, the names of matching types, and any qualifiers.

    6. If you want to create method stubs, select the check boxes of the ones that you want to create.

    7. For the code generation model, select

      Generic Action Class.

    8. Click Next.

  4. If you want to create an action mapping in addition to creating an action, leave the

    Add new mapping check box selected on the Create a mapping for your Action class page, and complete the following steps:

    1. Make sure that a configuration file name is specified.

    2. If you want to change the default mapping path, type the path name in the

      Mapping Path field.

    3. Optional:

      Specify the extends action mapping. You can extend a forward or exception by specifying the path of the action that you want to extend.

    4. If you want to specify a form bean, select the name from the

      Form Bean Name drop-down menu.

    5. If you want to specify a form bean scope, from the Form Bean Scope drop-down list select

      request or

      session. The default behaves as if

      request were selected.

  5. Click Finish.