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.
- web.xml File
- Struts.xml File
- Struts-config.xml File
- Struts.properties 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 = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
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"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <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> </action> <-- more actions can be listed here --> </package> <-- more packages can be listed here --> </struts>
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 –
The constant tag along with name and value attributes must be used to dissolve any of the following attributes that are defined in the default.properties, 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 default.properties 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-config||Configuration file’s root node|
|2. form-beans||To 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 forwards||To 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-mappings||A place to declare form handlers.|
|5. controller||Use to configure Struts internals.|
|6. plug-in||It 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" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <!-- Form Bean Definitions --> <form-beans> <form-bean name = "login" type = "test.struts.LoginForm" /> </form-beans> <!-- Global Forward Definitions --> <global-forwards> </global-forwards> <!-- Action Mapping Definitions --> <action-mappings> <action path = "/login" type = "test.struts.LoginAction" > <forward name = "valid" path = "/jsp/MainMenu.jsp" /> <forward name = "invalid" path = "/jsp/LoginView.jsp" /> </action> </action-mappings> <!-- Controller Definitions --> <controller contentType = "text/html;charset = UTF-8" debug = "3" maxFileSize = "1.618M" locale = "true" nocache = "true"/> </struts-config>
The Struts.properties File
For changing the default behavior of the framework, a mechanism is provided by this configuration file. All the properties of the struts.properties 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 default.properties 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 struts.properties 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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.