Struts 2 Configuration Files

Struts 2 Configuration Files

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

  1. web.xml File
  2. Struts.xml File
  3. Struts-config.xml File
  4. File
The web.xml file

The we.xml configuration file is basically a J2EE configuration file that defines how the processing of HTTP request elements are done by the servlet container. Though it is not rigorously a Struts2 configuration file, the configuration of this file is needed for the working of Struts2.

The web.xml file is the entry point of an application. Struts 2 application entry point will be a filter that is defined in the deployment descriptor(web.xml). Thus, in web.xml we define an entry point of FilterDispatcher class. Remember that, under the WebContent/WEB-INF folder the web.xml file should be created.

It is the first configuration file that you will require to configure if you are not starting with the help of a template or a tool that generates it (e.g. Eclipse or Maven2). Here is the content of the web.xml file used in the below example.

<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = ""
   xmlns = "" 
   xmlns:web = ""
   xsi:schemaLocation = ""
   id = "WebApp_ID" version = "3.0">
   <display-name>Struts 2</display-name>



Note: We need to map the Struts2 filter to /* but not /*.action. It means that the struts filter will parse all the URLs.

The Struts.xml File

The struts.xml file includes the configuration instruction which you will be remaking as actions are developed. This file may be used to dissolute default settings for an application, for example, struts.devMode = false and other settings that are defined in the property file. This file can be repressed under this folder WEB-INF/classes.

Let’s see an example of a struts.xml file:

<?xml version = "1.0" Encoding = "UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
      <action name = "hello" 
         class = "com.tutorialspoint.struts2.HelloWorldAction" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
      <-- more actions can be listed here -->

   <-- more packages can be listed here -->


The very first thing to look at is the DOCTYPE. All struts configuration files must have an accurate doctype as shown in the example. <struts> is the root tag element, inside which we are declaring various packages using <package> tags. It <package> allows separation and modularization of the configuration. This is meaningful when you have a very big project and that project is divided into various modules.

For example, if your project has three sections – business_application, customer_application, and staff_application, then you could create those three packages and keep the associated actions in the correct package.

The package tags are shown below –

Struts 2 Configuration Files

The constant tag along with name and value attributes must be used to dissolve any of the following attributes that are defined in the, like we set struts.devMode property. Setting struts.devMode property makes us watch more debug messages in the log file.

For every URL we have a desire to access, we need to define action tags and also a class that has an execute() method. This execution() method will be accessed when we are going to access the corresponding URL.

Results show what gets returned after an action is executed to the browser. The string that is returned from the action must be the name of a result. Results are constructed per-action as above, or as a “global” result that is available to every action in a package. Results have an optional name and type attributes. The default value name is “success”.

Struts.xml file can promote large over time and breaking it by packages is one way of modularizing it, but another way to modularize the struts.xml file is Struts. You could break the file into multiple XML files, import them in the following fashion.

The distinct configuration file which we are unable to cover is the struts-default.xml. It consists of standard configuration settings and mostly we don’t need to change any settings of this file. That is why we are not going too deep on this file. For having more knowledge, have a look into the file which is available in the struts2-core-2.2.3.jar file.

The Struts-config.xml File

It is the linker between the view and model components. Though most of the time we don’t need to change any settings of this file. The following is the main elements that this file contains –



1. struts-configConfiguration file’s root node
2. form-beansTo map your ActionForm class to a name. In the rest of the strutsconfig.xml file and in your JSP pages also, this name is used as an alias for ActionForm.
3. global forwardsTo map a page on your webapp to a name. It is used as a referral to the actual page. Using this, it helps you to avoid the hardcoded URLs on your web pages.
4. action-mappingsA place to declare form handlers.
5. controllerUse to configure Struts internals.
6. plug-inIt tells Struts about the location of the properties files that contain prompts and error messages.

The sample struts-config.xml file is given below –

<?xml version = "1.0" Encoding = "ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
   		<!-- Form Bean Definitions -->
      <form-bean name = "login" type = "test.struts.LoginForm" />
   	<!-- Global Forward Definitions -->
  	 <!-- Action Mapping Definitions -->
         path = "/login"
         type = "test.struts.LoginAction" >
         <forward name = "valid" path = "/jsp/MainMenu.jsp" />
         <forward name = "invalid" path = "/jsp/LoginView.jsp" />
   	<!-- Controller Definitions -->
   <controller contentType = "text/html;charset = UTF-8"
      debug = "3" maxFileSize = "1.618M" locale = "true" nocache = "true"/>
The File

For changing the default behavior of the framework, a mechanism is provided by this configuration file. All the properties of the configuration file can be configured using the constant tag within the struts.xml configuration file as well as by the init-param in the web.xml file. If you want to keep the things separately and much more specific to struts, then under the WEB-INF/classes folder you can create this file.

The default values that are already configured in the which is in the struts2-core-x.y.z.jar distribution, will be overridden by the configured values of this file. we can modify or change a couple of properties using the file –

#When set to true, Struts will act much more friendly for developers
struts.devMode = true

#Enables reloading of internationalization files
struts.i18n.reload = true

#Enables reloading of XML configuration files
struts.configuration.xml.reload = true

#Sets the port that the server is run on
struts.url.http.port = 8080

Note: Hash(#) denotes the comment lines only in the above code.

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

Leave a Reply

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