Struts.xml: The Default Settings for Your Strut Applications

struts.xmlThe Struts Framework is a standard for developing well-architected Web applications. Struts is an open source framework that extends the Java Servlet API and employs a Model, View, Controller (MVC) architecture. It enables you to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, JavaBeans, resource bundles, and XML. You can learn more about web development and how these technologies work together in this course.

Struts is most popular among the framework since it supports extensive validations where other frame works don’t, and it has inbuilt support for I18N. Struts 2 actions classes are Spring friendly so you can easily integrate. Over all, it is a good framework for front end based applications.

What is MVC?

MVC refers to the Model-Vide-Controller architecture. The model notifies the application’s state to Views and Controllers. This notification will allow the Views to get updated and controllers to modify the commands.

The view is responsible for displaying the output back to the user which is also called as presentation. Controller tells the View/Views to generate the output. In Struts the view layer is implemented using JSP/HTML. In short the View/Views is just what you see on the screen.

The controller tells the model to update its state (e.g., editing a document). Controller is the  component that contains all primary logic required, like sessions and security and complete flow of the applications. Also it handles all the request from the user and selects the appropriate view to return for the user (e.g., by scrolling through a document). In Struts the controller’s job is done by the ActionServlet class. Struts Configuration Files

The interactions between Model, View and Controller flow are taken care by the struts configuration files. These files are web.xml, struts.xml, struts-config.xml and struts.properties. Let’s take a quick look at these:

  • The web.xml file: The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. This file provides an entry point for any web application. The web.xml file needs to be created under the folder WebContent/WEB-INF.
  • The struts-config.xml file:The struts-config.xml configuration file is used to provide a link between the View and Model components in the Web Client. This stores application routing information and request mapping in a single core file, struts-config.xml.
  • The struts.properties file: This configuration file provides a mechanism to change the default behavior of the framework. You can create this file under the folder WEB-INF/classes.

You can take a look at the official documentation here. Now, lets get into the details of the struts.xml configuration file.

The Struts.xml File

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. This file can be created under the folder WEB-INF/classes. Before you move ahead, you may want to learn XML basics with this course.

The Struts.xml file contains tags such as:

The <XML> tag is used to define the version of xml that you are using for.

The<DOCTYPE> tag is used to refer to access standard Document Type Definition for struts.

The <Struts> tag is the root tag element when you are using Struts.

The next <Constant> tag along with name and value attributes will be used to override any of the following properties defined in default.properties.

The <Package> tag allows separation and modularization of the configuration. This is very useful when you have a large project and project is divided into different modules. The following are the attributes of Package tag:

  • 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 <action>tags corresponds to every URL you want to access and you define a class with execute() method which will be accessed whenever you will access corresponding URL

These are very similar to HTML tags (you can learn details of HTML tags in this course)

Sample Struts.xml Files

<?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="HelloWorldAction"   method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>

Output:

Displays HelloWorld.jsp page

In the above example, the first and second line describes definition of XML version and DTD. Then the default property of struts.devMode is set to value trueusing this setting struts.devMode you can see more debug messages in the log file. Next package is create with name “Helloworld”. Inside this package action was created, this will execute execute() method in “HelloWorldAction” class. If it is successful then it will display HelloWorld.jsp page.

You define action tags corresponding to every URL you want to access; and you define a class with execute() method which will be accessed whenever you will access corresponding URL. 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 have optional name and type attributes. The default name value is “success”.

How to Create Multiple Struts.xml Files

This is used to include multiple Struts configuration files into a single unit. When the Struts.xml file grow big over time and so breaking it by packages is one way of modularising it, but struts offers another way to modularise the struts.xml file. You could split the file into multiple xml files and import them in the following fashion:

Structs.xml

<?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>

my_struts1.xml

<?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"   method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>

my_struts2.xml

<?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="helloAmerica" extends="struts-default">
<action name="hello"   method="execute">
<result name="success">/HelloAmerica.jsp</result>
</action>
</package>
</struts>

In the above example, the first and second line describes definition of XML version and DTD.Using <include> tag you can include any number of package into a single file. This this case three files will be created that is my_struts1.xml, my_struts2.xml and structs.xml files.

my_struts1.xml-Put all struts1 module settings here.

my_struts2.xml- Put all struts2 module settings here.

Explanation for above two xml files are similar to the first example.

struts.xml – Put default settings and include the my_struts1.xmland my_struts2.xml.

The other configuration file that is covered is the struts-default.xml. This file contains the standard configuration settings for Struts and you would not have to touch these settings for 99.99% of your projects. If you are interested, take a look into the at the default properties file available in struts2-core-2.2.3.jar file.

Struts are very useful when you developing web based application.  We recommend that you peruse the official documentation of struts and you try out the programs. Of course, this works only in tandem with the other web development technologies – you may want to learn about them from scratch in this course.