Apache Struts 2 is a sophisticated, flexible framework for developing enterprise-class Java web applications. This framework is intended to simplify the whole development cycle, from application creation through deployment and maintenance over time.
This article will discuss the Struts.xml files and all the tags used in them in detail.
Struts.xml🎯
The struts.xml file provides the configuration information you will change when actions are created. This file may be used to alter an application's default settings and other settings described in the property file. This file is generated under the WEB-INF/classes folder.
Let's look at 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.codingninjas.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>
Let us discuss the various tags of Struts.xml and their attributes.
Package Tag💻
Our struts application may be divided into submodules. A module is specified via the package tag. The struts.xml file can include one or more packages. The various attributes of the package element are given below:
Constant Tag🎯
The constant tag, coupled with the name and value attributes, should be used to override any of the properties defined in default.properties, such as the struts.devMode property. When we set the struts.devMode property, we may see additional debug messages in the log file.
Action Tag🤔
We define action tags for each URL we wish to visit, and we construct a class with an execute() function that will be called whenever we reach the relevant URL.
Attributes of Action Tag
name: A name is required for specifying any activity.
class: Action has an optional attribute called class. If the class property is not specified, ActionSupport is assumed to be the default action. A simple action may be as follows:
<action name="product">
method: It is an optional property. If you do not supply a method attribute, the execute method will be used as the action class's method. So here's the code:
If you wish to call a specific method of the action, you must use the method attribute.
Result Tag🎯
The results determine what is returned to the browser once an action is completed. The action's return string should be the name of a result. As previously stated, results can be defined per action or as a "global" result available to all actions in a package. The name and type attributes of the results are optional. "success" is the default name value.
Attributes of Result Tag
name: It is a non-mandatory attribute. If the name property is not specified, success is presumed to be the default outcome name.
type: type is an optional property. If the type property is omitted, the dispatcher is presumed to be the default result type.
Note: Struts.xml files can expand in size over time. Thus splitting them up into packages is one approach to modularising them. However, Struts provides another option to modularise the struts.xml file. You might divide the file into many XML files and import them separately.
Frequently Asked Questions
Define package tag in struts.xml file.
Struts application may be divided into submodules. A module is specified via the package tag. The struts.xml file can include one or more packages.
What are the Struts 2 core components?
The three major Struts2 building components are the Action, Interceptor, and Result pages. Struts2 allows you to define and customise action classes in various ways.
What is Struts XML?
The struts.xml file contains the configuration data that will be changed when actions are generated. DevMode = false, as well as other default parameters provided in property files, can be altered for an application that uses this file, such as struts.
What is the function of the struts.xml configuration file?
The struts-config.xml configuration file connects the Web Client's View and Model components. It is crucial in the construction of both Controller components and Application-specific settings. This file is produced in Web NMS for each application and is named module>-struts-config.
Where should struts.xml be placed?
The default (struts.xml) file is the framework's fundamental configuration file, and it should be located on the web app's classpath (usually /WEB-INF/classes). Other configuration files may be included in the default file if needed.
Conclusion
This article has extensively discussed the Struts.xml file and its various tags.