Struts 2 Configuration File

In this tutorial you will learn about the Struts 2 Configuration File and its application with practical example.

Struts 2 Configuration File

Struts2 Application contains some configuration files such as web.xml, struts.xml, strutsconfig.xml and struts.properties. Out of these files we can start working with web.xml and struts.xml files.Let’s understand about these files.

web.xml: This configuration file is from J2EE configuration file which is used to determines how elements of the HTTP will be processed by the container. This file is the standard deployment descriptor for the Web Application. It declares the filters and servlets used by the service. Below is the file example of web.xml, which we have used in our struts2 example.

From here you can also define that which will be your welcome file.

Recommended:-  Struts 2 Adding Plugin

struts.xml

This is core configuration file for the framework which can initialize its own resources such as interceptors, Action classes, Results that can prepare views etc. Below is the example of struts2.xml file.

Description: Let’s understand this file,
1. DOCTYPE : All struts configuration file needs to have correct doctype as shown in above code i.e. “DOCTYPE struts PUBLIC”.

2.<struts>: This is the root tag element in which we can declare different packages using “<package>” tag.

3.<constant>: This tag is used for overrides any of the following properties defined in default.properties, as like I did
<constant name = “struts.devMode” value = “true” />.

4.<package>: This tag specifies a module. This tag is very useful when large project divided into different modules. There are some important attributes of package tag, let’s understand them

Attribute & Description:
a) name:
required field for this tag, This is unique identifier for the package.

  1. b) extends: By default we extends “struts-default”, extends keyword defines that which package does this package extend from.
  2. c) namespace: This attribute is an optional attribute of package. If there is no namespace is declare so by default it takes “/”.
  3. d) <action>: The action element is sub-element of Package. There some attributes defined in the action elements.
  • name: This is required attributes for defining name of the action.
  • class: This is an optional attribute of action tag. Here you can define your action class.
  • method: This is also an optional attribute of action tag. If you don’t define any method so by default it takes execute
  • <result>: This is sub-element of action tag, there are also some attributes are used in this element which are listed below.
  1. name: This is an optional attribute of result tag. If you are not using this attribute so by default it takes “success”.
  2. type: this is also an optional attribute of result tag. If not using this so dispatcher uses default result type.
Recommended:-  Struts 2 ActionContext

 

In this tutorial we have learn about the Struts 2 Configuration File and its application with practical example. I hope you will like this tutorial.