Introduction:
- Configuration files represent the actual working of any application. They show the basic initial settings for running any executable. A program before executing, consults the configuration files for the actual flow of working.
- In Struts 2 basically five configuration files are to be created. Of which, some of them are optional and some are necessary to create.
- The files for Struts2 configuration are:
- Web.xml
- Struts.xml
- Struts-config.xml
- Struts.properties
- The files web.xml and struts.xml are mandatory for running struts 2 applications. While the other three are created as per application requirement. We will study these files in our future applications.
Configuration File – web.xml:
- This file is not basically of Struts 2 Framework, but it is necessary for executing any web application in Java. This file gives configuration similar to public static void main (String args[]) in java. It specifies the entrance point of execution of a web application. It is also known as “DEPLOYMENT DESCIPTOR”.
- Web.xml is to be created under WebApplication/WEB-INF folder. If you are using IDE such as NetBeans or Eclipse, the file is automatically created. But you have to edit as following structure.
- The general format or template of web.xml is:
<?xml version = “” encoding = “” ?> <web-app> <display-name> Name of struts application </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> Fully Qualified Name of the first page to be executed </welcome-file> </welcome-file-list> </web-app>
- <?xml ?> Specifies that the page is of XML type and its version is 3.x. The encoding attribute is generally optional. It represents that the encoding is in Unified Text Format 8. That is encoding =”UTF-8”
- <web-app> … </web-app>: is the root element of web.xml. All other tags reside under this element.
- <display-name> .. </display-name>: this element is optional and it specifies the display name of the Struts 2 application. Used only for GUI.
- <filter> … </filter>: main element for other initialization tags.
- <filter-name> … </filter-name>: The filter name identifies the filter in current filter element as well as in other filter elements. It is similar to ID attribute used in forms.
- <filter-class> … </filter-class>: Specifies the initial configuration class name. The value is org.apache.struts2.dispatcher.FilterDispatcher. This class is already defined in org.apache.struts2.dispatcher package. It is used to setup the Struts 2 application. The initial configuration of how-to run application is specified in FilterDispatcher.
- <filter-mapping> … </filter-mapping>: The sub element used here is <filter-name> ... </filter-name>. The name of the filter should remain same as mentioned in previous element.
- <url-pattern> … </url-pattern>: The pattern of execution of the servlet is to be mentioned here. “/*” specifies the pattern is default. That is, the pattern which is specified in FilterDispatcher class. The default pattern is “.do”. You can customize it if necessary.
- <welcome-file-list> ... </welcome-file-list>: The element specifies list of welcome files. That is, which file is to be executed while the application starts is specified under this element using welcome-file tag.
- <welcome-file> … </welcome-file> : this element is used for state the welcome file name. Fully qualified name of the file is to be given in this element. For example, if the first page to be loaded is in a folder named JSP then the tag is written as:
<welcome-file> jsp/firstpage.jsp </welcome-file>
<?xml version = “3.0” encoding = “UTF-8”” ?> <web-app> <display-name> WebProject</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> jsp/firstpage.jsp </welcome-file> </welcome-file-list> </web-app>
Configuration File – struts.xml:
- The mapping of all the form actions is to be given in struts.xml file.
- On submit of which form, which Java class is to be followed and which method of that class is to be executed is specified in struts.xml.
- Struts.xml consists of mapping of form actions to Java classes and then two methods of these classes.
- Thus, on every submit button, there is an action and a mapping of these actions is given in struts.xml configuration file.
- This file is to be placed under WEB-INF/classes folder.
- The general format or template of struts.xml is:
<?xml version = “” encoding = “” ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name =”Name of Package” extends = “Name of super package” > <action name = ”Name of Action(as specified in form tag)” class = ”Class Name to be followed” method = “Method Name to be executed”> <result name = ”success/failure/anything”> FQN of JSP file to be loaded </result> </action> </package> </struts>
Explanation
- <?xml ?> Specifies that the page is of XML type and its version is 3.x. The encoding attribute is generally optional. It represents that the encoding is in Unified Text Format 8. That is encoding = “UTF-8”
- <!DOCTYPE struts>: Doctype is an optional tag (not an element) which is used to give identification that the current page is of document type struts. If you are using an IDE, this format will be automatically generated.
- <struts> … </struts>: is the root element of web.xml. All other tags reside under this element.
- <package> … </package> : the classes with similar functionality are generally grouped under a name of the package. This element is used to reference the classes with same functionality such as login-logout, session handling etc.
<package name = “Name of Package” extends = “Name of super package” >
- Here, name is the name of current package. That is, name of the package whose class we are referring to.
- Extends contain the name of super package, if any. For example:
<package name = “action_classes” extends = “allactions” > Here, the super package is “allactions”. This package can also contain n number of sub package. But our concern is for a class which is inside action_classes package.
- <action> … </action>: The action element actually maps the JSPs with action classes. The required attributes for action element are: name, class and method.
For example:
<action name = “click” class = “calculator” method = “addition”>
Here, the name of action must match with the name of the action attribute in the form tag in JSP. The action tag here specifies that on click on the submit button, the class calculator from action_classes package is to be followed and the method addition() is to be executed.
- <result> … </result> : result element has mainly two names : “success” and “failure”. These names are by default created by Struts. But you can also add customized result names in java classes.
- For example:
package action_classes; class calculator { private int x, y, ans; //getter and setter of x, y and ans variables public String addition() { if (x != 0 && y != 0) { ans = x + y; return “success”; } else { return “failure”; } } }
- So the result element can be given as
<result name = “success”> jsp/answer.jsp </result>
<result name = “failure”> jsp/firstpage.jsp </result>
- The string returned from the Java method is given as a name in result element. The result element with matching string will execute the jsp page.
- Thus, multiple result elements can be given for single action.
- One struts.xml can contain multiple package elements, one package element can contain multiple actions and one action element can contain multiple result elements.
Example of struts.xml:
<?xml version = “3.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> <package name = “default” extends = “struts-defaults” > <action name = “click” class = “calculator” method = “addition”> <result name = “success”> jsp/result.jsp </result> </action> </package> </struts>
Setting Multiple Configuration Files:
- A web application like a banking website or a student management system or a portal may contain multiple configurations.
- In order to maintain this properly, it may be required to divide struts.xml or web.xml configuration file into multiple files.
- But as the framework will always find struts.xml for execution, all the configuration files need to be embedded in struts.xml only.
- Say for example, we have following two configuration files
- additionStruts.xml
- subtractionStruts.xml
// additionStruts.xml <?xml version = “3.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> <package name = “default” extends = “struts-default” > <action name = “add” class = “Calculator” method = “addition”> <result name = “success”> jsp/result.jsp </result> </action> </package> </struts>
// subtractionStruts.xml <?xml version = “3.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> <package name = “default” extends = “struts-default” > <action name = “sub” class = “Calculator” method = “subtraction”> <result name = “success”> jsp/result.jsp </result> </action> </package> </struts>
//java file.. package action_classes; class calculator { private int x, y, ans; //getter and setter of x, y and ans variables public String addition(){ ans = x + y; return “success”; } }
- These two configuration files can be embedded in struts.xml by using the following syntax:
<include file = “name of file”></include>
- Include element is used to load any file (of any extension) in the current file. Just give FQN of that file in “file” attribute of this element.
- In our example, struts.xml will now be as follows:
<?xml version = “3.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> <package name = “default” extends = “struts-default” > <include file=" additionStruts.xml"></include> <include file=" subtractionStruts.xml"></include> </package> </struts>