Vous êtes sur la page 1sur 7

Struts 2.

Company Internal

Struts 2.0
What it is
Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time. Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

SLK Software Services

Company Internal 2 /7

Struts 2.0
Key Features
Build!

Improved Design - In contrast with Struts 1, all of the Struts 2 classes are based on interfaces. Core interfaces are HTTP independent. These APIs are not dependent on Servlet APIs. Enhanced Tags - Struts 2 tags don't just send output data, but also provide stylesheet-driven markup so that you can create consistent pages with less code. Struts 2 tags can now work with FreeMarker, Velocity, and similar template engines. Stateful Checkboxes - The Struts 2 framework automatically tracks checkboxes; if a checkbox is missing, the default value -- false -- is assumed. Hence, unlike in Struts 1, checkboxes do not require special handling for false values. AJAX support To create dynamic Web applications that use Ajax, Struts 2 provides an Ajax theme, which gives interactive applications a significant boost. Struts 2 tags for Ajax are based on Dojo widgets. Plug-ins are available for other frameworks as well. Easy Spring integration Struts 2 Actions are Spring-aware. Just adding Spring beans to an application will add Spring support. Enhanced Results - In Struts 2, the >forward< tag is replaced by the <result> tags. Unlike ActionForwards, Struts 2 results help in preparing the response and provide flexibility to create multiple types of output. Do more with speciality results for JasperReports, JFreeChart, Action chaining, and file downloading. POJO forms - Struts 2 does not support the ActionForms feature. The properties that are defined in the ActionForms can be put directly on the Action classes. You do not need to use all String properties. POJO Actions - Struts 2 Action classes are framework independent and are simplified plain old Java objects (POJO). Any Java class with an execute() method can be used as an Action class. Introduction of interceptors: Struts 2 provides exhaustive support for interceptors. Interceptors can be executed before and after an Action class is executed. Interceptors are configured to apply common functionality, such as workflow or validation, to a request. Use of annotations: Applications built using Struts 2 can use Java 5 annotations as an alternative to XML and Java properties configuration. Annotations minimize the need for XML. Annotations are available for Actions, interceptors, validation and type conversion
SLK Software Services Company Internal 3 /7

Struts 2.0
Key Features (Continued)
Deploy! Easy plugins - Struts 2 extensions can be installed by dropping the plug-in JAR file into the \WEB-INF\lib directory. No manual configuration is required. Bundled plugins add support for JavaServer Faces, JasperReports, JFreeChart, Tiles, and more QuickStart: Deployed configuration files are reloadable; hence, many changes can be made on the fly without the need to restart the Web container

Maintain! Easy-to-test Actions - Struts 2 Actions are HTTP independent and thus framework neutral. They can be tested easily without using mock objects. Intelligent Defaults - Most of the configuration elements in the Struts 2 configuration file will have default values, so there is no need to set values unless a different value is required. This helps reduce the configuration you need to do in the XML file. Struts 2's support for annotations helps make this even easier to achieve. Easy-to-customize controller - Struts 1 allows the request processor to be customized per module; in Struts 2, you can customize the request handling per action, if required. Integrating Debugging - Research problem reports with built-in debugging tools. Easy-to-tweak tags - Customize tag markup by editing a FreeMarker template.

SLK Software Services

Company Internal 4 /7

Struts 2.0
Struts 1.0 and 2.0 Comparison
# 1 Feature web.xml Struts 1 Uses <servlet /> and <servletmapping /> tags to map the controller, ActionServlet. Struts 1 actions can handle *.do URIs. Struts 2 Uses <filter /> and <filter-mapping /> tags to map the dispatcher, FilterDispatcher. Generally, Struts 2 actions are mapped to /* because /* is better to use with filters; but by default, Struts 2 actions can handle *.action. The default extension for Struts 2 actions can be set in the properties file. The default name of the configuration file is struts.xml. This file requires fewer elements to be configured than strutsconfig.xml does, and the elements need fewer attributes. Obsolete. Replaced with the <package /> tag. The attributes of this tag are simplified and the <forward> tag is replaced by the <result> tag. The <result> tag has intelligent defaults set; for instance, its default name is "success," its default path is the current location, and its default content is the MIME type specified. However, the default values can always be overridden. Obsolete. The properties are now part of the Action class itself and hence there is no need to have another input object.

URI pattern

Struts configuratio n file


<form-beans /> <actionmappings /> <action />

The default name of the configuration file is strutsconfig.xml. Used to specify the input handling. Used to specify the list of Action classes. The attributes in this tag are used to specify the Action class, form beans, validation, etc. The response page is specified by the <forward> tag.
ActionForms are used to capture input and they must inherit from a base class, which results in many redundant classes.

4 5 6

ActionForms

Action class

Extends from the abstract base class Action.

Generally extends from the base class ActionSupport and may implement the Action interface. The advantage here is that any POJO can be used as an Action.
Company Internal 5 /7

SLK Software Services

Struts 2.0
Struts 1.0 and 2.0 Comparison (Continued)
# 9 Feature Singleton Actions Struts 1 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 requests for that Action. Struts 1 Actions use the Servlet API and hence are HTTP dependent. A major limitation of Struts 1 is that Actions can't be tested separately. Uses standard JSP mechanism page context for binding objects to views. Supports validation through the Validator framework and with the validate method. Supports separate request processors for each module. Not supported. Struts 2 Struts 2 Actions are not singletons; they are instantiated for each request, and hence they need not be thread-safe.

10

HTTP independen ce of Action Unit testing

Struts 2 Actions are not coupled to a container and hence are HTTP independent. Struts 2 Actions can be tested easily with the support of dependency injection. Actions can be instantiated with the properties set and the methods invoked. Uses a value stack to bind objects to view. The advantage here is that the view that is rendered is not coupled with the type of the object. Supports validation through the XWork validation framework and the validate method. The XWork validation framework supports chaining of validation. Supports separate request handling for each Action.

11

12

Output to views Support for validation Controller customizati on Annotations

13

14

15

Supports Java 5 annotations, which can be used as an alternate to XML files.

SLK Software Services

Company Internal 6 /7

Struts 2.0
Conclusion
The Struts 2 framework features a simple architecture that is not hard to extend. It is smarter and easier to use than Struts 1 because it is POJO based. It also boasts a number of advantages over the previous release. The Struts 2 framework comes with rich set of features, such as enhanced Actions, enhanced tag support, support for Ajax, and support for annotations, all of which can be really helpful to the larger developer community. However, Struts 1 cannot be made obsolete immediately, as there are plenty of developers still using it. Migrating from Struts 1 to Struts 2 is not very difficult, but it involves some effort. The easiest approach to migration would be migrating one page at a time. Even though this is a simple idea, there are still many changes to make. We recommend that you use Struts 2 for new projects and continue to use Struts 1 for existing applications already written for that framework. However, both versions of the framework can co-exist within a single application.

SLK Software Services

Company Internal 7 /7

Vous aimerez peut-être aussi