Both versions of Struts hosting framework provide the following three key components:
• A request handler provided by the application developer that maps Java classes to standard URI.
• A response handler that maps control to server pages or other Web resources which then will be responsible for completing the response.
• A tag library that helps developers to creating rich, responsive, form-based applications with server pages.
We’ll be covering the following topics in this tutorial:
Difference between Struts1 and Struts2
1. ACTION CLASS
In Struts 1 Action classes extend an abstract base class. A common problem in Struts 1 is use abstract classes instead of interfaces.
In Struts 2 Action class may implement an Action interface, with other interfaces to enable require services. Struts 2 provides a base ActionSuppport class to implement commonly used interfaces. The Action interface is not required. Any POJO object with a execute can be used as an Struts 2 Action object.
2. Threading Model
Struts1 Actions are singletons and must be thread- safe since there will only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop. Action resources must be thread-safe or synchronized.
Struts2 Action objects are instantiated for each request, so there are no thread-safety issues. (In practice, servlet containers generate many throw away objects per request, and one more object does not impose a performance penalty or impact garbage collection.)
3. Servlet Dependency
In Struts1 the HttpServletRequest and HttpServlet Response is passed to the execute method when an Action is invoked as the as Actions have dependencies on the servlet API.
Struts2 Actions are not coupled to a container. Struts2 Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServlet Response directly.
4. Expression Language
Struts1 integrates several tag libraries, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support Tag libraries for JSP.
Struts2 can use JSTL single tag library that covers all, but the framework also supports a more powerful and flexible expression language called “Object Graph Notation Language” (OGNL) data transfer /type conversion.
5. Binding Values into Views
Struts1 uses the standard JSP mechanism for binding objects into the page context for access.
Struts2 uses a “ValueStack” technology so that the taglibs can access values without coupling. The ValueStack technique allows reuse of views across a range of types which may have the same property name but different property types.
6. Crosscutting for Action Form
Struts1 uses an ActionForm object to handle the input like Actions, HTML form is mapped to an ActionForm instance, all ActionForms must extend a base class. Since other JavaBeans cannot be used as ActionForms, developers often create redundant classes to handle input.
Struts2 uses Action properties as input properties, eliminating the need for a second input object. Input properties may be rich object types which may have their own properties. The Action properties can be accessed from the web page via the taglibs. Struts2 also support the ActionForm pattern, as well as POJO form objects and POJO Actions. HTML form maps directly to a POJO. Rich object types, including business or domain objects, can be used as input/output objects.
7. Control of Action Execution
Struts1 supports separate Request Processors or each module, but all the Actions in the module must share the same lifecycle.
Struts2 supports creating different lifecycles as per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.
8. Type Conversion
Struts1 Action Form properties are usually all Strings. Struts1 use Commons-Beanutils for type conversion. Converters are per-class, and not configurable per instance.
Struts2 use OGNL for type conversion. The framework includes converters for basic and common object types and primitives.