Struts 2 Elements

Struts 2 Elements

In this article, I am going to discuss Struts 2 Elements. Please read our previous article where we discussed Struts 2 Configuration Files. At the end of this article, you will understand the following pointers in detail.

  1. Package element
  2. Attributes of the package element
  3. Action element
  4. Attributes of the action element
  5. Result element
  6. Attributes of the result element
  7. Other elements
Struts 2 Package Element

It is the sub-element of struts, which represents a module of an application. Usually, it extends the struts-default package, which contains many interceptors and result types.

Attributes of a Package Element



name must for explaining any package.
namespace(optional) If the namespace is absent, / is assumed as the default namespace. Then in such cases, we have to invoke the action class if you need this type of URI: /actionName.action

If you define any namespace, you need this URI: /namespacename/actionName.action

extend The package element mostly extends the struts-default package in which result types and interceptors are defined. If you spread out struts-default then all the actions of this package can easily use the interceptors and result-types explained in the struts-default.xml file.
Struts 2 Action Element

It defines the sub-element of a package, which represents an action to invoke for the incoming request. It has name, class, and method attributes. If you forget to specify the name attribute, then the execute() method is called for the specific action class by default.

Attributes of an Action Element
Attribute Description
name must for explaining any action.
class(optional) If you delete the class attribute, then ActionSupport will be taken as the default action. A simple action may be follows as: <action name=”Login”>
method(optional) 1. If the method attribute is not mentioned, then the execute() method will be thought of as the method of action class. The code for this: <action name=”Login” class=””>

2. You need to specify the method attribute for the invocation of a particular method of the action.

The code for this: <action name=”Login” class=”” method=”execute”>

Struts 2 Result Element

It means the sub-element of action, which represents a view to be invoked. Struts framework shows for the returned string by the action class, if it returns success, then the result page for the action is called whose name can be success or without any name. And the optional attributes are name and types. If you forgot to specify the result name, then success is considered as a result name by default. By mistake, If you forgot to define the type attribute, then by default, the dispatcher is assumed as the default result type.

Attributes of Result Element
Attribute Description
name(optional) Omitting the name attribute, success is accepted as a default result name.
type(optional) Omitting the type attribute, the dispatcher is accepted as a default result type.
Other Elements

Besides the above elements, there are plenty of elements like include, global-results, global-exception-mappings, etc.

In the next article, I am going to discuss the Multiple Configuration File. Here, in this article, I try to explain Struts 2 Elements and I hope you enjoy this Struts 2 Elements article.

Leave a Reply

Your email address will not be published. Required fields are marked *