Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🤔
2.
Struts.xml🎯
3.
Elements of Struts.xml👨🏻‍💻
3.1.
Package Tag💻
3.2.
Constant Tag🎯
3.3.
Action Tag🤔
3.3.1.
 Attributes of Action Tag
3.4.
Result Tag🎯
3.4.1.
 Attributes of Result Tag
4.
Frequently Asked Questions
4.1.
Define package tag in struts.xml file.
4.2.
What are the Struts 2 core components?
4.3.
What is Struts XML?
4.4.
What is the function of the struts.xml configuration file?
4.5.
Where should struts.xml be placed?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Struts.xml

Introduction🤔

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. 

struts coding ninjas

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>

 

Must Read Apache Server

Elements of Struts.xml👨🏻‍💻

Let us discuss the various tags of Struts.xml and their attributes.

struts.xml coding ninjas

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: 

package tag struts coding ninjas

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:
     
<action name="product" class="com.javatpoint.Product">  


will be the same as:

<action name="product" class="com.javatpoint.Product" method="execute">  

 

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. 

Do you not feel eager to read/explore additional information on the subject of Struts 2 after reading about the Struts.xml file and its various tags? See the Struts 2 documentationStruts Tutorial for beginnersStruts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!🥷

coding ninjas
Live masterclass