About struts.xml
The core configuration file for the framework is the default (
There are two complementary approaches. We can include other
You can use
A "module" can be added to an application by placing a
<?xml version="1.0" encoding="UTF-8" ?>
struts.xml) file and should reside on the classpath of the webapp (generally /WEB-INF/classes).- The default file may include other configuration files as needed.
- A
struts-plugin.xmlfile can be placed in a JAR and automatically plugged into an application, so that modules can be self-contained and automatically configured.- In the case of Freemarker and Velocity modules, the templates can also be loaded from the classpath, so the entire module can be plugged in as a single JAR.
Break up a large struts.xml file into smaller pieces
There are two complementary approaches. We can include other
struts.xml-format files from a bootstrap struts.xml file, or we can package a struts.xml files in a JAR. Or both. By Include
You can use <include> elements in your struts.xml interchangeably with <package>
elements. The configuration objects will be loaded in the order of
appearance. The framework reads the configuration from top to bottom and
adds objects as they are referenced.
By JAR
A "module" can be added to an application by placing a struts.xml
and related classes into a JAR on the classpath. FreeMarker and
Velocity templates can also be provided by JAR, making it possible to
distribution a module in a single, self-contained JAR that is
automatically configured on startup.
Sample struts.xml File
<?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="default" extends="struts-default">
<action name="product" class="com.data.Product">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
1) package element
We can easily divide our struts application into sub modules. The package element specifies a module. You can have one or more packages in the struts.xml file.Attributes of package element
- name name is must for defining any package.
- namespace 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:
If you specify any namespace, you need this URI:
- /actionName.action
- /namespacename/actionName.action
- extends The package element mostly extends the struts-default 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 struts-default.xml file.
2) action element
The action is the subelement of package and represents an action.Attributes of action element
- name name is must for defining any action.
- class class is the optional attribute of action. If you omit the class attribute, ActionSupport will be considered as the default action. A simple action may be as:
- <action name="product">
- method It is an optional attribute. If you don't specify method attribute, execute method will be considered as the method of action class. So this code:
will be same as:
- <action name="product" class="com.data.Product">
If you want to invoke a particular method of the action, you need to use method attribute.- <action name="product" class="com.data.Product" method="execute">
3) result element
It is the sub element of action that specifies where to forward the request for this action.Attributes of result element
- name is the optional attribute. If you omit the name attribute, success is assumed as the default result name.
- type is the optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.