Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Struts 2 New Features

Struts 2 New Features

       Recently for a  training I started learning struts2.  As I had worked on struts one few years back I started comparing both struts-2 and 1. I found struts2 very easier than struts1.  

Though struts 1 was very popular, comparing it with the newer frameworks, which has come into market in last 2 years, struts 1 has some problems.

  •  ActionForms / Action classes etc – one action needed lots of coding and they were a pain
  • Also the action class unit testing was difficult.
  • Type conversions were not automatic.
  • There was no special feature for AJAX in the framework.. etc

          Strut 2 has come up with new features to overcome all above problems. The strut 2.0 is an integration of Struts 1.0 with Webwork. The basic changes in struts 2 are struts 2 Action classes and interceptors. In struts 2 a filter named ‘FilterDispatcher’ acts as controller to dispatch the request to separate action class instance.  There is no more ActionForms in Struts 2. The view will directly pull data from action class (as it happens in JSF). Beans can be used inside Action class as properties.

 The Struts 2 framework doesn’t impose any much ways of formal requirements on the actions classes. Any class can be an action if it wants. It simply must provide an entry method for the framework to invoke when the action is executed. Struts 2 actions don’t have to implement the Action interface. Any POJO object with a execute signature can be used as a Struts 2 Action object.

The struts 2 actions do three things. First is encapsulating the actual work to be done for a given request. The second major role is to serve as a data carrier in the framework’s automatic transfer of data from the request to the view.  Finally, the action must assist the framework in determining which result should render the view that’ll be returned in the request response.

      These make ActionForms to disappear from Struts 2. The Data will be pulled from the Action classes directly making the process simpler.

  Another new feature in Struts 2 is introduction of interceptors. Interceptors are Struts 2 components that execute both before and after the rest of the request processing. They provide an architectural component in which to define various workflow and cross-cutting tasks so that they can be easily reused as well as separated from other architectural concerns.

Logging is a good example. Logging should be done with the invocation of every action, but it probably shouldn’t be put in the action itself. Because it’s not part of the action’s own unit of work. It’s more administrative, overhead if you will. Struts 2 uses interceptors to both separate and reuse these cross-cutting concerns. 

Another difference in struts 1 and struts-2 is the threading model. Struts one Action classes were singletons (one instance of Action class serving all the requests for that action). This requires extra care in some cases. Where as in Struts-2 the action classes are instantiated per request. Each request will be served by different thread. 

The Struts 2 action classes do not depend upon the servlet API (The execute method of the struts 1 needs HttpServletRequest and Response objects as parameter.). Hence they can be easily tested by instantiating them.

 Few other new features of Struts 2 are

  1. Its expression language. It can still use JSTL but It also introduces a powerful expression language OGNL (Object Graph Notation Language).
  2. Type conversion in Struts 1 was always a pain. But Struts2 using OGNL expression langage can automatically convert basic object types and primitives.
  3. Also Struts 2 has introduced AJAX theme to make the webapplication more dynamic.
  4. It also support configuration of action classes using annotations. Also it provides easy integration with Spring and Hibernate.

 All these new feature make struts 2 more attractive. One draw back of struts 2 is, as its bit new, the community using struts 2 is small.  But the powerful features make development in struts 2 much easier.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
Categories: Programming / tutorials Tags:
  1. February 17th, 2011 at 10:07 | #1

    Latha
    There are 3 tests on Struts . If you go on home page and search, you should be able to find that.
    But these 2 tests cover some aspects of Struts 2
    http://www.skill-guru.com/test/127/struts-interview-questions-2
    http://www.skill-guru.com/test/126/struts-interview-questions-1
    You should find them helpful

  2. latha
    February 17th, 2011 at 00:28 | #2

    i want interview questions on struts 2. please share with me

  3. Naveen
    February 12th, 2010 at 01:35 | #3

    Gud one

  4. Deepak
    November 5th, 2009 at 01:06 | #4

    Thanks Sir

  5. November 4th, 2009 at 07:42 | #5

    Yes Deepak. More posts are coming soon.

  6. Deepak
    November 4th, 2009 at 03:44 | #6

    very informative blog,I am using struts1.3, and i also want to know more about struts 2 so plz share some training topics about struts 2.

  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes