Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices

By: Authors: Shenoy S. Mallya N. Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The configurable controller is the answer to the Fat controller problem. In a Fat Controller, the programmers can code “if” blocks on need basis. Not so with the configurable controllers. The expressive and configuration capability is limited to what the built-in controller can support. In Struts, the built-in controller supports a variety of cases that can arise while developing web applications. It even provides points to extend the configuration capabilities. These points known as Extension points, take the configurationcapability to the next dimension. In this tutorial, we will just look at the normal facilities offered by the strutsconfig.xml.

The Struts configuration file adheres to the struts-config_1_1.dtd. The struts config dtd can be found in the Struts distribution in the lib directory. It shows every possible element, their attributes and their description. Covering all of them at once would only result in information overload. Hence we will only look at the five important sections of this file relevant to our discussion and their important attributes. In fact we have already covered most of these in the lifecycle discussion earlier, but are summarizing them again to refresh your mind.

The five important sections are:
1. Form bean definition section
2. Global forward definition section
3. Action mapping definition section
4. Controller configuration section
5. Application Resources definition section

Listing below shows a sample Struts Config file showing all the five sections. The form bean definition section contains one or more entries for each ActionForm. Each form bean is identified by a unique logical name. The type is the fully qualified class name of the ActionForm. An interesting to note is that you can declare the same ActionForm class any number of times provided each entry has a unique name associated with it. This feature is useful if you want to store multiple forms of the same type in the servlet session.

Table: Important attributes and elements of ActionMapping entry in struts-config.xml

Attribute/Element name Description
Path  The URL path (either path mapping or suffix mapping) for which this Action Mapping is used. The path should be unique
Type  The fully qualified class name of the Action
Name  The logical name of the Form bean. The actual ActionForm associated with this Action Mapping is found by looking in the Form-bean definition section for a form-bean with the matching name. This informs the Struts application which action mappings should use which ActionForms.
Scope  Scope of the Form bean – Can be session or request
Validate  Can be true or false. When true, the Form bean is validated on submission. If false, the validation is skipped.
Input  The physical page (or another ActionMapping) to which control should be forwarded when validation errors exist in the form bean.
Forward  The physical page (or another ActionMapping) to which the control should be forwarded when the ActionForward with this name is selected in the execute method of the Action class.

The ActionMapping section contains the mapping from URL path to an Action class (and also associates a Form bean with the path). The type attribute is the fully qualified class name of the associated Action. Each action entry in the action-mappings should have a unique path. This follows from the fact that each URL path needs a unique handler. There is no facility to associate multiple Actions with the same path. The name attribute is the name of the Form bean associated with this Action. The actual form bean is defined in Form bean definition section. Table above shows all the relevant attributes discussed so far for the action entry in action-mappings section.

//Sample struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
<struts-config> Form bean Definitions
<form-bean name="CustomerForm"
<form-bean name="LogonForm"
</form-beans> Global Forward Definitions
<forward name="logon" path="/logon.jsp"/>
<forward name="logoff" path="/logoff.do"/>
</global-forwards> Action Mappings
<action path="/submitDetailForm"
<forward name="success"
redirect=”true” />
<forward name="failure"
path="/Failure.jsp" />
<action path=”/logoff” parameter=”/logoff.jsp”
type=”org.apache.struts.action.ForwardAction” />
</action-mappings> Controller Configuration
processorClass="org.apache.struts.action.RequestProcessor" />
<message-resources parameter="mybank.ApplicationResources"/>
</struts-config> Message Resource Definition

In the ActionMapping there are two forwards. Those forwards are local forwards – which means those forwards can be accessed only within the ActionMapping. On the other hand, the forwards defined in the Global Forward section are accessible from any ActionMapping. As you have seen earlier, a forward has a name and a path. The name attribute is the logical name assigned. The path attribute is the resource to which the control is to be forwarded. This resource can be an actual page name as in

<forward name="logon" path="/logon.jsp"/>

or it can be another ActionMapping as in

<forward name="logoff" path="/logoff.do "/>

The /logoff (notice the absence of “.do”) would be another ActionMapping in the struts-config.xml. The forward – either global or local are used in the execute() method of the Action class to forward the control to another physical page or ActionMapping.

The next section in the config file is the controller. The controller is optional. Unless otherwise specified, the default controller is always the org.apache.struts.action.RequestProcessor. There are cases when you want to replace or extend this to have your own specialized processor. For instance, when using Tiles (a JSP page template framework) in conjunction with Struts, you would use TilesRequestProcessor.

The last section of immediate interest is the Message Resource definition. In the ActionErrors discussion, you saw a code snippet that used a cryptic key as the argument for the ActionError. We stated that this key maps to a value in a properties file. Well, we declare that properties file in the struts-config.xml in the Message Resources definition section. The declaration in Listing above states that the Message Resources Bundle for the application is called ApplicationResources.properties and the file is located in the java package mybank.

If you are wondering how (and why) can a properties file be located in a java package, recall that any file (including class file) is a resource and is loaded by the class loader by specifying the package.

Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment


View Tutorial          By: epuri at 2008-09-09 01:05:56
2. View Comment


View Tutorial          By: Sujit at 2009-01-12 22:38:08
3. View Comment

Good explanation... very helpfull

View Tutorial          By: Ananth at 2009-02-02 00:26:05
4. View Comment

Very good. Keep it up.

View Tutorial          By: jk at 2009-05-23 11:00:29
5. View Comment

Good Article. Small and Sweet...

View Tutorial          By: Anupam Pawar at 2009-05-27 03:04:08
6. View Comment

Nice article

View Tutorial          By: Chakravarthy at 2009-06-09 23:29:09
7. View Comment

short and very clear

View Tutorial          By: ramesh at 2009-07-22 22:27:25
8. View Comment

fitst read fully and put comments

View Tutorial          By: prasant at 2009-10-07 06:16:19
9. View Comment

Different from the others,Good One

View Tutorial          By: Amiiiii java slayer at 2009-10-14 08:10:26
10. View Comment

crystal clear explanation. He's explained very nicely. short and sweet explaination.

View Tutorial          By: jay at 2009-12-08 10:28:14
11. View Comment

Good article

View Tutorial          By: Manu Francis Mathew at 2009-12-18 00:50:45
12. View Comment

Thank alot

View Tutorial          By: Naganatarajan at 2010-01-07 05:54:17
13. View Comment

ok good.But what about plug-in's Information.

View Tutorial          By: Ramarao at 2010-01-31 07:32:02
14. View Comment

Excellent.. simple and too good to understand

View Tutorial          By: venkat at 2010-05-11 00:30:02
15. View Comment

Good one

View Tutorial          By: Soumya Anoop at 2010-05-13 23:23:44
16. View Comment

very good

View Tutorial          By: pravin gajbhiye, nagpur at 2010-05-24 01:43:30
17. View Comment


View Tutorial          By: gummalla subbarao at 2011-03-09 01:24:58
18. View Comment


View Tutorial          By: udhay at 2011-04-01 02:48:25
19. View Comment

good;Every one can understand

View Tutorial          By: seshu at 2011-04-26 05:11:09
20. View Comment

good tutorial for beginners..........

View Tutorial          By: Madarapu naveen at 2011-06-18 11:19:50
21. View Comment

good explanation.

View Tutorial          By: praneeth at 2011-06-20 23:28:00
22. View Comment


View Tutorial          By: Rohan at 2011-11-03 08:25:59
23. View Comment

good one

View Tutorial          By: thyagesh at 2011-12-06 05:06:27
24. View Comment

Nice one really helpful thanks

View Tutorial          By: Shashikant at 2012-01-06 12:00:45
25. View Comment

great work! such a super explanation.. thanks a lot:)

View Tutorial          By: vinu dominic at 2012-02-08 06:16:48
26. View Comment

very good

View Tutorial          By: Deepika at 2012-03-26 07:00:29
27. View Comment

nice one

View Tutorial          By: navyasri at 2012-05-02 04:41:10
28. View Comment

Very nice

View Tutorial          By: sharath at 2012-07-17 11:31:20
29. View Comment


View Tutorial          By: santosh at 2012-08-28 06:12:44
30. View Comment

Excellent explanation...Thanku.

View Tutorial          By: Rajpal Singh at 2012-12-29 05:36:20
31. View Comment

Thanks a lot!!!

Its very useful explaination.

View Tutorial          By: dipali at 2012-12-31 06:49:41
32. View Comment

Thank you for giving info.

View Tutorial          By: Deepak cheela at 2013-01-23 07:03:08
33. View Comment

Thanks a ton! :)

View Tutorial          By: Prateek at 2015-03-12 11:40:08

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Authors: Shenoy S. Mallya N.
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
ForwardAction in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts
Handling Duplicate Form Submissions in Struts
Chaining actions in Struts
Configuring JDBC DataSources in Struts
Editing web.xml in a Struts Application
Creating the first application using Struts 2
Struts 1 vs Struts 2
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
Struts and Tiles - Steps to use Struts and Tiles
Creating Struts Modules
Using Multibox in Struts
Use ImageButtonBean for Image based Form Submits in Struts
Most Emailed Articles (in Struts)
What is a Model View Controller (MVC) Model?
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
History of Struts
WebWork 2 is now Apache Struts 2
Configuring JDBC DataSources in Struts
Struts Classes
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2
Struts 1 vs Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)