trainingtrains Logo

91-9990449935

 0120-4256464

Struts 2 Configuration File

The struts application contains two main configuration files struts.xml file and struts.properties file.

The struts.properties file is used to override the default values of default.xml file provided by struts framework. So it is not mandatory. Mostly, you will not use struts.properties file. We will learn about it later.

Here, we are going to learn all about struts.xml file. First of all let us see the simple example of struts.xml file

struts.xml

1) package element

We can easily divide our struts application into sub modules. The package element specifies a module. You can have one or more packages in the struts.xml file.

Attributes of package element

  • name name is must for defining any package.
  • namespace It is an optional attribute of package. If namespace is not present, / is assumed as the default namespace. In such case, to invoke the action class, you need this URI:
    If you specify any namespace, you need this URI:
  • extends The package element mostly extends the struts-default package where interceptors and result types are defined. If you extend struts-default, all the actions of this package can use the interceptors and result-types defined in the struts-default.xml file.

2) action element

The action is the subelement of package and represents an action.

Attributes of action element

  • name name is must for defining any action.
  • class class is the optional attribute of action. If you omit the class attribute, ActionSupport will be considered as the default action. A simple action may be as:
  • method It is an optional attribute. If you don't specify method attribute, execute method will be considered as the method of action class. So this code:
    will be same as:
    If you want to invoke a particular method of the action, you need to use method attribute.

3) result element

It is the sub element of action that specifies where to forward the request for this action.

Attributes of result element

  • name is the optional attribute. If you omit the name attribute, success is assumed as the default result name.
  • type is the optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.

Other elements

There are many other elements also such as global-exception-mappings, global-results, include etc. It will be discussed later.