Sunday 21 August 2011

Struts 2

With so many choices out there, why choose Struts2? Here are
some of the features that may lead you to consider Struts2:
Action based framework
Mature with a vibrant developer and user community
Annotation and XML configuration options
POJO-based actions that are easy to test
Spring, SiteMesh and Tiles integration
OGNL expression language integration
Themes based tag libraries and Ajax tags
Multiple view options (JSP, Freemarker, Velocity and
XSLT)
Plug-ins to extend and modify framework features
Of all the decisions in choosing a framework, choosing the style
of framework is going to be the most controversial. Let’s take a
look at how we got to today’s web application options, and
where Struts2 fits into the picture.

Servlets
Servlets provided the first Java-based foray in to web
development. Following the HTTP protocol, servlets provide a
way to map a URL to a special class whose methods would be
called.
It was quickly recognized that although this was a large step
forward, generating the HTML code from within the Java code
was a maintenance nightmare. Each time a simple user interface
change was needed, the Java developer needed to modify the
Servlet code, recompile the source and then to deploy the
application into the server environment.

JSP and Scriptlet Development
As a result of this “maintenance nightmare,” the style of
development was turned upside-down. Rather than placing the
HTML code within the Servlet or Java code, the Java code was
placed (as script-lets) inside the HTML code – as Java Server
Pages (JSP). Each JSP provided both the logic for processing of
requests, and the presentation logic.
One problem was solved, but another was introduced. The Java
code is the same as used in class files; however there is no
structure of methods or classes. Looking through early JSP files,
you would find one of two things:
Cut-and-pasted code – Java code that has been copied
from one JSP, to another, and another, etc. Propagating
any defects or errors from the original code, and
increasing the amount of work required to make a
common change.
Calling common Java formatting objects – common
formatting or logic code was incorporated into a reusable
object. Each JSP then used the common object.
From these findings a best practice as well as a pattern emerged
– use Java objects from JSPs.
As the JSP specification evolved, tags were introduced to
encapsulate re-usable java objects. Tags provided a HTML-like
façade for accessing the underlying code, allowing designer
(rather than developers) and IDEs to interact with dynamic
elements to compose page layouts. Examples of the tags
provided by JSP are <jsp:useBean … /> and
<jsp:getProperty … />. Along with the provided JSP tag
libraries, JSPs provided a way for developers to create their own
tag libraries.

Action-Based Frameworks
Action based frameworks came onto the scene to combine the
concepts of servlets and JSPs. The idea being to split the request
processing for the page the user sees into processing logic and
the presentation logic, letting each part do what it does the best.
The implementation used a pattern from Smalltalk known as the
model-view-controller pattern – or more recently known as the
front controller, or in Sun parlance Model 2.
In this pattern the servlet is the controller, providing a
centralized point of control for all client page requests. It maps
the request URL to a unit of work know as an action. The
action’s job was to perform specific functionality for a given
URL by accessing the HTTP session, HTTP request and form
parameter, calling business services, and then mapping the
response into a model, whose form is a plain old java object.
Finally, the action returned a result, which was mapped (via
configuration files) to a JSP to render as the view.
Struts2 is an action based MVC web framework.
Component-Based Frameworks
As web applications became more complex, it was realized that
a page was no longer the logical separation – web applications
had multiple forms per page, links for content updates and many
other custom widgets – all which needed processing logic to
perform their tasks.
To address these complexities, component based frameworks
have become popular. They provide a close tie between user
interface components and classes that represent the components,
and they are event-driven and more object orientated than action
based frameworks. A component could be a HTML input field,
a HTML form or custom widgets provided by or created for the
framework. Events, such as form submits or links, are mapped
to methods of the class representing the component, or to special
listener classes. An additional benefit of component based
frameworks is that they allow you to re-use components across
multiple web applications. Examples of component based
frameworks are JSF, Wicket and Tapestry.
The Great Equalizer – Ajax
In the beginning of 2005, a new fascination was starting in web
development. Coined by Jesse James Garrett, Ajax stood for
“Asynchronous JavaScript and XML.” Relatively speaking, the
technologies were nothing new. In fact, the primary web
browser components for making the asynchronous calls – the
XMLHttpRequest Object – had already been available for 6 years
(since version 5 of Internet Explorer).
But what was new was the application of the technology.
Google Maps was one of the first applications to take full
advantage of the technology. The web page had come alive –
you could interact with controls and widgets. By using a mouse
you could scroll maps around the screen; when entering an
address, the information would materialize above the maps
images; and finally, the culmination being route planning which
orchestrated all these features into a useable web application.
And all of this happened without a single page refresh!
User interfaces with Ajax functionality allows the web browser
to make requests to the server for smaller amounts of
information, and only when it is needed. The result from the
server request is formatted or manipulated and applied directly
to the page being displayed, with the web browser passing on the
changes to the user. Only the sections of the page that changed
are re-rendered, not the entire page, making the user feel that the
web application is more responsive to their actions.
The requests from the UI act like events – they are more
discrete, conveying information for a single component or
function. No longer does a single action need to retrieve data for
the entire page, they can be more concise and thus more reusable
across applications. In effect, an Ajax user interface
calling an action based framework allows the action framework
to behave in a similar manner to a component based framework.
In fact, this combination of technologies provides a more
loosely-coupled and more re-usable system. The same actions
can provide JSON, XML or HTML fragment views for the Ajax
components as well as being combined with other actions to
provide HTML views for non-Ajax user interfaces
  • Build!
    • Easy startup - Jumpstart new projects with our bootstrap tutorial and template application or Maven archetype.
    • Improved Design - Code clean against HTTP-independant framework interfaces.
    • Enhanced Tags - Code less with stylesheet-driven form tags that provide their own markup.
    • Stateful Checkboxes - Avoid special handling with smart checkboxes that know when they are toggled.
    • Flexible Cancel Buttons - Go directly to a different action on cancel.
    • First-class AJAX support - Add interactivity and flexibility with AJAX tags that look and feel just like standard Struts tags.
    • Easy Spring integration - Inject dependencies into Actions using Spring without glue code or red tape. (Plexus support also available.)
    • Enhanced Results - Do more with speciality results for JasperReports, JFreeChart, Action chaining, and file downloading.
    • POJO forms - No more ActionForms! Use any JavaBean to capture form input or put properties directly on an Action class. Use both binary and String properties!
    • POJO Actions - Use any class as an Action class -- even the interface is optional!
  • Deploy!
    • Easy plugins - Add framework extensions by dropping in a JAR. No manual configuration required! Bundled plugins add support for JavaServer Faces, JasperReports, JFreeChart, Tiles, and more ...
    • Integrated profiling - Peek inside Struts2 to find where the cycles are going!
    • Precise Error Reporting - Flip directly to the location and line of an error.
  • Maintain!
    • Easy-to-test Actions - Test Struts2 Actions directly, without resorting to mock HTTP objects.
    • Intelligent Defaults - Skip obvious and redundant settings. Most framework configuration elements have a default value that we can set and forget. Say it once!
    • Easy-to-customize controller - Customize the request handling per action, if desired. Struts2 only does what you want it to do!
    • Integrating Debugging - Research problem reports with built-in debugging tools.
    • Easy-to-tweak tags - Customize tag markup by editing a FreeMarker template. No need to grok the taglib API! JSP, FreeMarker, and Velocity tags are fully supported.

No comments:

Post a Comment