CSS Drop Down Menu

Friday, 17 January 2014

Struts 2.x: Configuration File

The struts application contains two main configuration files struts.xml file and struts.properties file.The struts.properties file is

used to override the default values of default.xml file provided by struts framework. So it is not mandatory. Mostly, you will not use

struts.properties file. We will learn about it later.Here, we are going to learn all about struts.xml file. First of all let us see the simple

example of struts.xml file.

1:- package element

We can easily divide our struts application into sub modules. The package element specifies a module. You can have or or more

packages in the struts.xml file.
Attributes of package element:-

[a]-  name is must for defining any package.

[b]-  namespace 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:    /actionName.action

If you specify any namespace, you need this URI:    /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:-

[a]:-name is must for defining any action.

[b]:-classclass 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:



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:

3:- result element

It is the sub element of action that specifies where to forward the request for this action.
Attributes of result element:-

[a]-name is the optional attribute. If you omit the name attribute, success is assumed as the default result name.

[b]-type is the optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.


Why Struts 2.x???

Or

Difference between Struts1.x and Struts2.x


Let us see the component and functional differences between struts 1.x and struts 2.x
[a]- In struts 1.x front controller is ActionServlet while In 2.x front controller is FilterDispatcher.

[b]- In struts 1.x we have RequestProcessor class In 2.x we have Interceptors instead RequestProcessor will see about this

concept later just remember as of now.

[c]- In struts 1.x we have multiple tag libraries like, html, logic, bean..etc. In 2.x we do not have multiple libraries, instead we have

single
library which includes all tags.

[d]- In struts 1.x the configuration file name can be [any name].xml and we used to place in WEB-INF-folder. In 2.x

the configuration file must be struts.xml only and this must be in classes folder or WEB-INF/Classes/.

[e]- In struts 1.x we have form beans and Action classes separately In 2.x form bean, Action classes are combindly given as

Action class only, of course we can take separately if we want .

[f]- In struts 1.x properties file must be configured in struts-config.xml. But in 2.x we need to configure our resource

bundle(s)
in struts.properties file.

[g]- In struts 1.x we have programmatic and declarative validations only In 2.x we have annotations support too along

with programmatic and declarative validations.

Functional Differences:-

[a]:-In struts 1.x declarative validations are done by using validation frame work. In 2.x , declarative validations are done by using

xwork2 frame work
. [b]:- In struts 1.x an Action class is a singleton class, so Action class object is not a thread safe, as a

programmer we need to

make it as thread safe by applying synchronization In 2.x an Action class object will be created for each request, so it is by default

thread safe
, so we no need to take care about safety issues here.

[c]:- In struts 1.x we have only jsp as a view technology while In 2.x we have support of multiple view technologies like velocity,

Freemarker, jasper reports, jsp etc.


[d]:- In struts 1.x Action class is having servlet dependency, because in execute() method accepts req,res parameter

right.
In 2.x Action class doesn’t have any servlet dependency, because its execute() method doesn’t accepts any

parameters,
however we can access all servlet objects with dependency injection .

FilenameDescription
struts2-core.jar Framework library itself, found in distribution root directory
xwork.jar XWork 2 library on which Struts 2 is built (version 2.0 or later)
ognl.jar Object Graph Navigation Language (OGNL), the expression language used throughout the framework
freemarker.jar All UI tag templates are written in Freemarker (also a good option for your own views)
commons-logging.jar Commons logging, which the framework uses to support transparently logging to either Log4J or JDK 1.4+
commons-fileupload.jar The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.
commons-io.jar Commons IO is a library of utilities to assist with developing IO functionality.
web.xml Java web application configuration file that defines the filters (and other components) for your web application
struts.xml Framework configuration file that defines the actions, results, and interceptors for your application
Note:-
If any Struts 2 Plugins are included, then other JARs may be needed too. For example, the optional Spring Plugin requires the Spring JARs to be present.

0 comments:

Post a Comment