<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>This chapter will take you through basic configuration required for a Struts 2 application. Here we will see what will be configured in few important configuration files : <i><b>web.xml, struts.xml, struts-config.xml and struts.properties</b></i></p>
<p>The struts application contains two main configuration files <i><b>struts.xml</b></i> file and <i><b>struts.properties</b></i> file.<br />
The struts.properties file is used to override the default values of<i> <b>default.xml</b></i> file provided by struts framework. So it is not mandatory. Mostly, you will not use struts.properties file.</p>
<h2><b>The <i>web.xml</i> file:</b></h2>
<p>The <b><i>web.xml</i></b> configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.</p>
<p>As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (<i><b>web.xml</b></i>). Hence we will define an entry of <i><b>FilterDispatcher </b></i>class in web.xml. The <i><b>web.xml</b></i> file needs to be created under the folder <i><b>WebRoot/WEB-INF</b></i>.</p>
<p>This is the first configuration file you will need to configure if you are starting without the aid of a template or tool that generates it (such as Eclipse or Maven2). Following is the content of web.xml file which we used in our last example.</p>
<h2><i><b>web.xml</b></i></h2>
</div>
<pre class="highlight"><;?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>;Struts2MyFirstApp<;/display-name>; 
 <;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>; 
 <;welcome-file-list>; 
 <;welcome-file>;Login.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
<h2><b>The </b><b><i>struts</i></b><b><i>.xml</i> file:</b></h2>
<p>Here, we are going to learn all about <i><b>struts.xml</b></i> file. First of all let us see the simple example of <i><b>struts.xml</b></i> file. Let us have a look at the <i><b>struts.xml</b></i> file we created in the <b>Hello World </b>example explained in previous chapter.</p>
<p>The struts.xml file contains the configuration information that you will be modifying as actions are developed. This file can be used to override default settings for an application, for example struts.devMode = false and other settings which are defined in property file. This file can be created under the folder WEB-INF/classes.</p>
<p><b><i>struts.xml</i></b></p>
</div>
<pre class="highlight"><;?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.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="login" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;Welcome.jsp<;/result>; 
 <;result name="error">;Login.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
<;/struts>; 
</pre>
<p>The first thing to note is the <b>DOCTYPE</b>. All struts configuration file need to have the correct doctype as shown in our little example. <i><b><;struts>;</b></i> is the root tag element, under which we declare different packages using<i><b> <;package>;</b></i> tags.</p>
<h2><b>Multi Configuration File-</b></h2>
<p>Here <i><b><;package>;</b></i> allows separation and modularization of the configuration. This is very useful when you have a large project and project is divided into different modules.</p>
<pre class="highlight"><;?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>; 
 <;include file="my-struts1.xml"/>; 
 <;include file="my-struts2.xml"/>; 
<;/struts>; 
</pre>
<h2><b>Struts 2 Multiple Namespace Example-</b></h2>
<p>We can define multiple namespaces in struts.xml file by the namespace attribute of package element. As we know, default namespace is / (root).</p>
<p>Let&#8217;s see the simple example to define multiple namespaces in struts.xml file.</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" ?>; 
<;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts 
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">; 
<;struts>; 
 
<;package name="default1" namespace="/" extends="struts-default">; 
<;action name="hello" class="com.dineshonjava.struts2.login.LoginAction">; 
<;result>;welcome.jsp<;/result>; 
<;/action>; 
<;/package>; 
 
<;package name="default2" namespace="/first" extends="struts-default">; 
<;action name="hello" class="com.dineshonjava.struts2.login.LoginAction">; 
<;result>;welcome.jsp<;/result>; 
<;/action>; 
<;/package>; 
 
<;package name="default3" namespace="/second" extends="struts-default">; 
<;action name="hello" class="com.dineshonjava.struts2.login.LoginAction">; 
<;result>;welcome.jsp<;/result>; 
<;/action>; 
<;/package>; 
 
<;/struts>; 
</pre>
<h3><i><b>1) package element-</b></i></h3>
<p>We can easily divide our struts application into sub modules. The package element specifies a module. You can have or or more packages in the struts.xml file. Say, if your project has three domains &#8211;<b> business_applicaiton, customer_application and staff_application</b>, you could create three packages and store associated actions in the appropriate package.</p>
<p><b>Attributes of package elements:</b></p>
<ul>
<li><b>name</b> name is must for defining any package.</li>
<li><b>namespace</b> It is an optional attribute of package. If namespace is not present, / is assumed as the default namespace. In such case, to invoke the action class, you need this URI:
<div class="codeblock">
<pre class="highlight">/actionName.action 
</pre>
</div>
<p>If you specify any namespace, you need this URI:</p>
<div class="codeblock">
<pre class="highlighthighlight">/namespacename/actionName.action 
</pre>
</div>
</li>
<li><b>extends</b> The package element mostly extends the <b>struts-default</b> package where interceptors and result types are defined. If you extend struts-default, all the actions of this package can use the interceptors and result-types defined in the <b>struts-default.xml</b> file.</li>
</ul>
<h3><i><b>2) action element:</b></i></h3>
<p>The action is the sub-element of package and represents an action.<br />
<b>Attributes of action element-</b></p>
<ul>
<li><b>name</b> name is must for defining any action.</li>
<li><b>class</b> class is the optional attribute of action. If you omit the class attribute, <b>ActionSupport</b> will be considered as the default action. A simple action may be as:
<pre class="highlight"><;action name="login">; 
</pre>
</li>
<li><b>method </b>It is an optional attribute. If you don&#8217;t specify method attribute, execute method will be considered as the method of action class. So this code:
<pre class="highlight"><;action name="login" class="com.dineshonjava.struts2.login.LoginAction">; 
</pre>
<p><b>will be same as: </b></p>
<pre class="highlight"><;action name="login" class="com.dineshonjava.struts2.login.LoginAction" method="execute">; 
</pre>
<p>If you want to invoke a particular method of the action, you need to use method attribute.</li>
</ul>
</div>
<h3><i><b>3) result element:</b></i></h3>
<p>It is the sub element of action that specifies where to forward the request for this action.<br />
<b>Attributes of result element-</b></p>
<ul style="text-align: left;">
<li><b>name </b>is the optional attribute. If you omit the name attribute, success is assumed as the default result name.</li>
<li><b>type </b>is the optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.</li>
</ul>
<p> ;</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/writing-struts-2-hello-world-application/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts-2-namespace-configuration-example/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/writing-struts-2-hello-world-application/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts-2-namespace-configuration-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '393'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…