Techie Baba

Wednesday, December 19, 2007

Struts 2.0 Explained in Brief!

HighLights of Struts 2.0:
  • Action classes are simplified POJOs (Plain Old Java Objects). Any Java class with execute() method can be used as an Action class.
  • Struts 2 doesn't supports the ActionForm.
  • Most of the configuration elements in the Struts 2 configuration have default values, so there is no need to set values unless you need to change the default value. This helps reducing the configuration requirement.
  • Interceptors: Struts 2 introduces a new concept in the framework i.e. interceptors. Interceptors can be executed before and after an Action class is executed. Interceptors are configured to apply common functionality to a request like validation, checking session, etc. All the request pass through a set of interceptors before they are sent to an Action class and after the execution of Action class request passes through the interceptors again in the reverse order.
  • Struts 2 tags provides support for AJAX.
  • Framework automatically tracks checkboxes; if a checkbox is missing, the default value "false" is assumed.
  • Plug-in: Struts 2 extensions can be installed by just dropping the jar files into /WEB-INF/lib directory. No manual configuration is required.

Architecture Explained:



Fig 1. The diagram describes the framework's architecture.
Struts 2 is based on standard technologies such as Java filters, JavaBeans, ResourceBundles, Locales and XML, as well as few open source packages such as OGNL(Object Graph Navigation Language) and XWork.

How Struts 2.0 Framework works (in brief):
  • An initial request goes to the servlet container where it passes though a chain of filters.
  • Request passes through an optional ActionContextCleanUp filter (useful if integrating with other technologies).
  • Then the FilterDispatcher (required) is called, which uses the ActionMapper to determine whether there is a need to invoke an action for this request. If yes, it delegates the control to the ActionProxy.
  • ActionProxy utilizes the framework configuration files manager, which is initialized from the struts.xml file. ActionProxy creates an ActionInvocation, which is responsible for implementing the command pattern.
  • The ActionInvocation process invokes the required interceptors and then invokes an Action.
  • Once the Action is executed, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
  • Result is then executed, which, most of the time, renders a JSP or template written using FreeMarker or Velocity.
  • Interceptors are executed again after completing the Action in the reverse order.
  • If the ActionContextCleanUp filter is configured, the FilterDispatcher will not clean up the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not configured, the FilterDispathcer will clean up all the ThreadLocals present.
NOTE : (ActionContext has the complete details of the runtime request and response. The framework uses ThreadLocal in connection with the ActonContext class to make the configuration and other runtime details available.)

Major Differences from Struts 1:

  • Filters have replaced servlets in Struts 2. In web.xml the tags and tags to map the controller, ActionServlet has been replaced by and tags to map the dispatcher, FilterDispatcher.
  • ActionForms are obsolete now. Properties are part of the Action class.
  • The default name of configuration file is struts.xml (this file should reside on the classpath of the webapp, generally /WEB_INF/classes).
  • Struts 1 Actions are singletons and hence need to be thread-safe, since only one instance of the class will be available to handle all request for that Action. Struts 2 Actions are not singletons; they are instantiated for each request, and hence they need not be thread-safe.
  • Struts 1 Actions use the Servlet API and hence are HTTP dependent. Struts 2 Actions are not coupled to a container and hence are HTTP independent.
  • Unit Testing: In Struts 1 Actions can't be tested separately. Struts 2 Actions can be tested easily with the support of dependency injection. Actions can be instantiated with the properties set and the method invoked.
  • Struts 2 supports Java 5 annotations, which can be used as an alternate to XML files.
There are quite few more changes

This article has now moved to my new BLOG.

Labels: , , , ,

1 Comments:

Post a Comment

<< Home