Struts 1.x Vs Struts 2.x

Struts 2.x is very simple as compared to struts 1.x, few of its excelent features are:

1. Servlet Dependency:

Actions in Struts1 have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse objects are passed to the execute method when an Action is invoked but in case of Struts 2, Actions are not container dependent because they are made simple POJOs. In struts 2, the servlet contexts are represented as simple Maps which allows actions to be tested in isolation. Struts 2 Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly.

2. Action classes

Programming the abstract classes instead of interfaces is one of design issues of struts1 framework that has been resolved in the struts 2 framework.
Struts1 Action classes needs to extend framework dependent abstract base class. But in case of Struts 2 Action class may or may not implement interfaces to enable optional and custom services. In case of Struts 2 , Actions are not container dependent because they are made simple POJOs. Struts 2 provides a base ActionSupport class to implement commonly used interfaces. Albeit, the Action interface is not required. Any POJO object with an execute signature can be used as an Struts 2 Action object.

3. Validation

Struts1 and Struts 2 both supports the manual validation via a validate method.
Struts1 uses validate method on the ActionForm, or validates through an extension to the Commons Validator. However, Struts 2 supports manual validation via the validate method and the XWork Validation framework. The Xwork Validation Framework supports chaining validation into sub-properties using the validations defined for the properties class type and the validation context.

4. Threading Model

In Struts1, Action resources must be thread-safe or synchronized. So Actions are singletons and thread-safe, there should 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 Struts1 Actions and requires extra care to develop. However in case of Struts 2, 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.)

5. Testability

Testing Struts1 applications are a bit complex. A major hurdle to test Struts1 Actions is that the execute method because it exposes the Servlet API. A third-party extension, Struts TestCase, offers a set of mock object for Struts1. But the Struts 2 Actions can be tested by instantiating the Action, setting properties and invoking methods. Dependency Injection support also makes testing simpler. Actions in struts2 are simple POJOs and are framework independent, hence testability is quite easy in struts2.

6. Harvesting Input

Struts1 uses an ActionForm object to capture input. And all ActionForms needs to extend a framework dependent base class. JavaBeans cannot be used as ActionForms, so the developers have to create redundant classes to capture input.
However Struts 2 uses Action properties (as input properties independent of underlying framework) that eliminates the need for a second input object, hence reduces redundancy. Additionally in struts2, Action properties can be accessed from the web page via the taglibs. Struts 2 also supports the ActionForm pattern, as well as POJO form objects and POJO Actions. Even rich object types, including business or domain objects, can be used as input/output objects.

7. Expression Language

Struts1 integrates with JSTL, so it uses the JSTL-EL. The struts1 EL has basic object graph traversal, but relatively weak collection and indexed property support. Struts 2 can also use JSTL, however it supports a more powerful and flexible expression language called “Object Graph Notation Language” (OGNL).

8. Binding values into views

In the view section, Struts1 uses the standard JSP mechanism to bind objects (processed from the model section) into the page context to access. However Struts 2 uses a “ValueStack” technology so that the taglibs can access values without coupling your view to the object type it is rendering. The ValueStack strategy allows the reuse of views across a range of types which may have the same property name but different property types.

9. Type Conversion

Usually, Struts1 ActionForm properties are all Strings. Struts1 uses Commons-Beanutils for type conversion. These type converters are per-class and not configurable per instance. However Struts 2 uses OGNL for type conversion. The framework includes converters for basic and common object types and primitives.

10. Control Of Action Execution

Struts1 supports separate Request Processor (lifecycles) for each module, but all the Actions in a module must share the same lifecycle. However Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions as needed.

Advertisements

Spring vs Struts 2.0

While creating J2EE applications, it needs to be decided some times which technology we should go for – Struts or Spring. This is some points comparison between Spring and Struts.

1. In most of the key features compared, Spring MVC and Struts 2 are equally good, except in the UI supports. It seems to me Spring MVC just provides tags for basic HTML form tags while Struts/WebWork 2 provides many out-of-box JSF-styled complex and composite tags, such as:
a) Struts 2 integrates with Dojo AJAX framework closely and provides many complex UI components out-of-box, such as datepicker, tooltips, etc.
b) Struts 2 has AJAX theme.
c) Struts 2 tags are stylesheet-driven, making it easier to develop consistent pages.
d) Struts 2 checkboxes are stateful, and require no special handling.
The recent release of Spring 2.0 does not seem to have any work on this area.

2. Struts is mainly a presentation layer framework, like redirecting to a particular page ,doing client side validations etc which otherwise very tedious using jsp and servlets. Spring is a complete J2EE framework having seven independent layers which can be used all together or few layers integrated with some other framework. Spring provides declarative transaction management and AOP. One layer of spring is for presentation purpose like struts but for only presentation layer, struts is better than spring.

3. Struts has been around a long time and has been popular for years – there’s a wealth of knowledge about it in the user community and more literature around

4. If you want a bunch of taglibs that generate form fields and so forth, Struts is probably the better choice.

5. Our UI is mostly click-driven and light on data and validation. It seems to me that most people run into difficulties with Struts when they start moving a lot of data from HTTP into the model. We didn?t have that problem .

6. Spring does not present a framework for implementing business/domain logic. It helps you create a Controller and a View for your application, no Model though. Spring is a fully fledged application framework, that has a large stack of sub projects.

7. As for spring, if we look at its history, it started as an effort for supplying a solution for enterprise java applications, and to replace EJBs (while it also provided integration with EJBs for people who didn’t want to ditch that). Spring is much more than an MVC framework. It provides dependency injection, AOP, integration with almost every known framework, a security framework (a separate sub-project), and a lot of other stuff. Struts 2 and hibernate integrate easily with spring, so you might like to add it to your stack, without ditching struts 2
8. If you’re going to use Spring for other middleware services, and you have no Struts expertise in house, then it would be simplest to go with SpringMVC. If you know Struts well or have team members who do, or you already have a Struts project, it may make sense to stick with it.

So, basically, it depends on the requirements and the resources what and how you want to achieve which will decide what is to be used, Stuts 2.0 or Spring.

Struts 2 Framework

In software development process no framework is excellent or best. Although a better approach to structuring all the components such as objects, resources and the most importation part is managing them over the time of development of application or even after the product is released. It seems a too much expectation with a virtual world ;) .

Struts 2 framework has almost all the capabilities to handle starting from a small application which can be opted as quick as possible and Also got strong based on which developers like us can created almost enterprise level applications. Stuts framework is based on MVC approach. I must share with .NET people that the ASP.NET MVC 3 not even near about Struts 2. I knew there are many .net lover’s in market as I was. But let me tell you struts 2 framework has much more capabilities than asp.net mvc.

The conclusion: I am not here to list you folks the advantages of struts over asp.net nor pit falls.

Struts 2 framework automate the common tasks and provides a platform for the users to build applications quickly. Struts 2 is based on the OpenSymphony Web Works Framework. Struts 2 framework implements the Model-View-Controller (MVC) design pattern. Struts 1.x was the first world class and very simple implementation of MVC pattern. Although As I said there is no framework which is the best or complete framework. Struts 1.x was too popular and adapted by various developer. And then the inventor of Struts framework come up with a new version of framework i.e. Struts 2.

Now, as I have already created so much virtual comment over Sturts 2. Let’s roll further and get in to basic terms about struts 2 framework based on Model View and Controller design pattern.

Note: For all those who have not worked with MVC design pattern. I would recommend that you should do the google for understanding the pattern.

Struts 2 framework basic component

In nutshell struts is based on mvc design pattern. And we will talk about Struts 2 framework with its basic component to keep in mind.

Basic component are Model, View and Controller so here with struts we call them as action, result and FilterDispatcher respectivly.

When any request is made by client’s browser it is sent to FilterDispatcher(Controller). And Here FilterDispatcher plays its role and deiced which Action to process this request. Now just for instance consider FilterDispatcher has processed and it has forwarded the request to a Valid Action. So, Here the game begins and once the action is executed, a return(response) from a action that is called as result. This generated result then sent back to client’s browser and this way the process completes. It is pretty simple 😉

Now here is some equation:
Model = Model contains the data and the business logic. In Struts 2 the model is implemented by the Action component.
Controller = FilterDispatcher
View = Struts 2 the view is commonly implemented using JSP, Velocity Template, Freemaker or some other presentation-layer technology.

FilterDispatcher process the request and do the following:
Struts 2, The framework creates an instance of this action and associate it with the newly created instance of the ActionInvocation.

  • Invocation of action have to pass through a series of interceptors as defined(Optional) in the application’s XML file
  • Each time the invoke() method is called, ActionInvocation consults its state and executes whichever interceptor comes next.
  • ActionInvocation hands control over to the interceptor in the stack by calling the interceptors intercept() method.
  • The intercept() method of the interceptor inturn calls the invoke() method of the ActionInvocation till all the interceptors are invoked, in the end the action itself will be called and the corresponding result will be returned back to the user.

Some interceptor do work before the action is executed and some do work after the action is executed. It’s not necessary that it should do something each time it is invoked.

  • These interceptors are invoke both before and after the action.
  • First all the interceptors are executed in the order they are defined in the stack.

Then the action is invoked and the result is generated.

Again all the interceptors present in the stack are invoked in the reverse order.

The other important features of Struts 2 are OGNL and ValueStack.

Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack.

OGNL help in data transfer and type conversion