Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Action classes Struts1 extends the abstract base class by its action
class. The problem with struts1 is that it uses the abstract classes rather than
interfaces. While in Struts 2 an Action class implements an Action interface along
with other interfaces use optional and custom services. Struts 2 provides a base
ActionSupport class that implements commonly used interfaces. Although an Action
interface is not necessary any POJO object along with an execute signature can be
used as an Struts 2 Action object.
Threading Model Struts 1 Actions are singletons therefore they must be thread-
safe because only one instance of a class handles all the requests for that Action.
The singleton strategy restricts toStruts 1 Actions and requires extra care to make
the action resources thread safe or synchronized while developing an application.
Struts 2 doesn't have thread-safety issues as Action objects are instantiated for
each request. A servlet container generates many throw-away objects per request
and one more object does not impose a performance penalty or impactgarbage
collection.
Testability Struts1 application has a major problem while testing the application
because the execute method exposes the Servlet API. Struts TestCase provides a
set of mock object for Struts 1. To test the Struts 2 Actions instantiate the Action set
the properties and invoking methods. Dependency Injection also makes testing
easier.
Expression Language Struts1 integrates with JSTL so it uses the JSTL EL. The EL
has basic object graph traversal but relatively weak collection and indexed property
support. Struts 2 can use JSTL but the framework also supports a more powerful and
flexible expression language called "Object Graph Notation Language" (OGNL).
Binding values into views Struts 1 binds objects into the page context by using
the standard JSP mechanism. Struts 2 uses a ValueStack technology to make the
values accessible to the taglibs without coupling the view to the object to which it is
rendering. The ValueStack strategy enables us to reuse views across a range of
types having same property name but different property types.
Type Conversion Struts 1 ActionForm properties are almost in the form of Strings.
Commons-Beanutils are used by used by Struts 1 for type conversion. Converters
are per-class which are not configurable per instance. Struts 2 uses OGNL for type
conversion and converters to convert Basic and common object types and
primitives as well.
Validation Struts 1 uses manual validation that is done via a validate method on
the ActionForm or by using an extension to the Commons Validator. Classes can
have different validation contexts for the same class while chaining to validations
on sub-objects is not allowed. Struts 2 allows manual validation that is done by
using the validate method and the XWork Validation framework. The Xwork
Validation Framework allows chaining of validations into sub-properties using the
validations defined for the properties class type and the validation context.
When we deploy our application in the server, at first the container reads the information from
web.xml file.Here ActionServlet object will be created and init() of ActionServlet will be
called.Here ActionServlet is the backbone to the wholeapplication.
When client send a rewuest using .jsp extension , getters() and reset() of FormBean will be
called. When client fill the form and press on submit button, then setters() and validate() will be
called.
If the data is not valid ,then the form redirects to another page which is specified in struts-
config.xml file. If the data is valid , then only Action class object will be created.
In Action class , have execute() which have return type of ActionForward. We can specify the
business logic in Model and provide that object in execute().
After completion of business logic execution , then it forwards to another page ( either success or
failure) , whichis specified in struts-config.xml file.
But if your ActionMessages object is null or it contains the value it will goes to forwarded
action only. ActionMessages are used to display the warnings
in struts project to handle the error objet by using ActionError object and to handle the errors by
using ActionErrors object.
for suppose
aes.add(ae1);
aes.add(ae2);
to handle exception:
<global-exceptions>
<data-sources/>
<form- beans/>,
<global- forwards/>,
<actionmappings/>,
<exception/>,
<message-resources>,
<switch-action>
<plug-in/>
The main control file in the Struts framework is the struts-config.xml XML file where
action mappings are specified. This file's structure is described by the struts-config
DTD file which is defined at http://jakarta.apache.org/struts/. A copy of the DTD can
be found on the /docs/dtds subdirectory of the framework's installation root
directory. The top-level element isstruts-config. Basically it consists of the following
elements:
form-beans—A set of form-bean elements that describe the form beans that this
application uses