Tuesday, 12 April 2016

What are the main elements in struts.xml file ?


The struts tag is the root tag for the struts.xml. It may contain the tags :  package, include, bean and constant.

Package Tag 

Packages groups actions, results, result types, interceptors, and interceptor-stacks into a logical configuration unit.
Attribute name in package tag acts as the key for later reference to the package.
The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages - including all interceptor, interceptor-stack, and action configurations.
1. name : Unique name of package
2. extends : Package name that this package will extend
    All configuration information (including action configurations) from the extended package will be available in the new package, under the new namespace.
3. namespace : Mapping from the URL to the package
    i.e. for 2 different packages, with namespace attributes defined as "pack1" and "pack2", 
    the URLs would be something like "/webApp/pack1/my.action" and "/webApp/pack2/my.action"
4. abstract : if this attribute value is "true" the package is truly a configuration grouping and actions configured will not be accessible via the package name. It is important to make sure you are extending the correct parent package so that the necessary pre-configured features will be available to you.


Include Tag

The tag is used to modularize a Struts2 application that needs to include other configuration files.
Attribute "file" provides name of XML file to be included.
Included file has exactly the same structure as the "struts.xml".
Example : To break a configuration file of a finance application, you might choose to group together the invoices, admin, report configurations etc into separate files.


Bean Tag

Attribute class (mandatory) specifies Java class to be created or manipulated.
A bean can either be created by the framework's container and injected into internal framework objects, or have values injected to its static methods.
The first use, object injection, is generally accompanied by the type attribute, which tells the container that which interface this object implements.
The second use, value injection, is good for allowing objects not created by the container to receive framework constants. Objects using value inject must define the the static attribute.
  1. class - the name of the bean class
  2. type- the primary Java interface this class implements
  3. name- Unique name of this bean
  4. scope- Scope of the bean, either default, singleton, request, session, thread
  5. static- whether to inject static methods or not


Constant Tag

There are 2 key roles for constants.
1. They are used to override settings 
    Example Maximum file upload size, devMode (Development mode) settings etc.
2. They specify which Bean should be chosen, among multiple implementations of a given type.

Constants can be declared in multiple files.
By default, constants are searched for in the following order, allowing for subsequent files to override by the previous ones :
struts-default.xml  >  struts-plugin.xml  >  struts.xml  >  struts.properties  >  web.xml

No comments:

Post a Comment

Note: only a member of this blog may post a comment.