This chapter will give you the details about basic configuration required for a Struts 2 application. Here you can analyse how important configuration files will be configured: web.xml, struts.xml, struts-config.xml and struts.properties
Frankly said that the user can survive using web.xml and struts.xml configuration files and this can be estimated by the use of these two files that has been worked in the previous chapters. But, for the user’s knowledge let other files can be explained as well.
The web.xml configuration file is a J2EE configuration file that determines as to how the elements of the HTTP request are processed by the servlet container. It is not basically a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.
As it was 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 (web.xml). Hence, an entry of Filter Dispatcher class in web.xm has to be defined. The web.xml file should be created under the folder WebContent/WEB-INF.
This is the first configuration file that the user 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 was used in the last example.
Note that the map of the Struts 2 filter to /*, and not to /*.action which means that all urls will be parsed by the struts filter. This will be covered when you will go through the Annotations chapter.
The struts.xml file contains the configuration information that the user needs to modify the developed actions. 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.
Let us have a look at the struts.xml file that was created in the Hello World example explained in previous chapter.
The first thing we need to note is the DOCTYPE. All struts configuration file must have the correct doctype as shown in the example. <struts> is considered as the root tag element, under which the user declares different packages using <package> tags. Here <package> allows separation and modularization of the configuration. This is very useful helpful when you are provided with a large project and that project will be divided into different modules.
Say, for example if your project has three domains - business_applicaiton, customer_application and staff_application, you could create three packages and that created packages could store associated actions in the appropriate package. The package tag has the following attributes shown below:
|name (required)||The unique identifier for the package|
|extends||Which package does this package extend from? By default, we use struts-default as the base package.|
|abstract||If marked true, the package is not available for end user consumption.|
|namespace||Unique namespace for the actions|
The constant tag along with name and value attributes will be used to override any of the following properties defined in default.properties, like we just set struts.devMode property. Setting struts.devMode property allows the user to see more debug messages in the log file.
We define action tags that corresponds to every URL that the user want to access and we define a class with execute() method which will be accessed viseversa.
Results determine what gets returned to the browser after an action is executed. The string returned from the action should be the name of a result. Results are configured per-action as above, or as a "global" result, available to every action in a package. Results can be determined with optional name and type attributes. The default name value is set to "success".
Over the time Struts.xml file can grow very big, so it needs to break by packages to modularize it. This is the one method and the other way to modularize struts.xml file is to split the file into multiple xml files and import the file in the following way-
There is the other configuration file that was not covered is the struts-default.xml. This file contains the standard configuration settings for Struts and the user need not have to touch these settings for 99.99% of your projects. For this reason, we are not going into too much detail on this file. If you are interested, take a look into the at the default.properties file available in struts2-core-2.2.3.jar file.
The struts-config.xml configuration file is a link between the View and Model components in the Web Client but you would not have to touch these settings for 99.99% of your projects. The configuration file basically contains following main elements:
|SN||Interceptor & Description|
Following is the sample struts-config.xml file:
For more detail on struts-config.xml file, kindly check your struts documentation.
This configuration file provides a mechanism to change the default behavior of the framework. Actually all of the properties contained within the struts.properties configuration file can also be configured in the web.xmlusing the init-param, and also using the constant tag in the struts.xmlconfiguration file. But if the user likes to keep the things separate and more struts specific then this file can be created under the folder WEB-INF/classes.
The values configured in this file will override the default values configured in default.properties which is contained in the struts2-core-x.y.z.jar distribution. There are a couple of properties that need to consider to change using 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
Here you can see the line starting with hash (#) in every sentence which will be assumed as a comment and it will be ignored by Struts 2.
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.