Thursday, February 23, 2012

Struts2 why more powerful than Struts1. Struts2 Vs Struts1 . Compare between Struts1 and Struts2


STRUTS2 = STRUTS1 + Interceptor (=XWork) + OGNL - FormBean

1. Easy web.xml

STRUTS1:


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
 
STRUTS2:

<filter>
<filter-name>webwork</filter-name>
<filter-class>
   org.apache.struts.action2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


No struts-config.xml inside WEB-INF folder.

2. Easy Action Class.

STRUTS1:
public class MyAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        // do the work
        return (mapping.findForward("success"));
    }
}

All actions have to be thread-safe, as only a single action instance is created.
Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action.
Because the actions have to be thread-safe, all the objects that may be needed in the processing of the action are passed in the method signature.

STRUTS2:
Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
public class MyAction {
   public String execute() throws Exception {
        // do the work
        return "success";
   }
}

Here no need to extends other class and no need to pass every thing in execute method.

If you want to use any session or request ro response object then use xxxxAware, example:
public class MyAction implements ServletRequestAware {
   private HttpServletRequest request;
   public void setServletRequest(HttpServletRequest request) {
        this.request = request;
   }
   public String execute() throws Exception {
        // do the work using the request
        return Action.SUCCESS;
   }
}

3. No Action From

STRUTS1:
Struts 1 uses an ActionForm object to capture input. Like Actions, all ActionForms must extend a base class. Since  other JavaBeans cannot be used as ActionForms, developers often create redundant classes to capture input. DynaBeans can used as an alternative to creating conventional ActionForm classes, but, here too, developers may be redescribing existing JavaBeans.

STRUTS2:
All data of ActionForm will do here in action class, so need to create one more class.The Action properties can be accessed from the web page via the taglibs.

4. Advanced Expression Language:

STRUTS1:
Struts 1 integrates with JSTL, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support.

STRUTS2:
Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).

5. Easy Validation

STRUTS1:
Struts 1 supports manual validation via a validate method on the ActionForm, or through an extension to the Commons Validator.

STRUTS2:
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.

Content Source.
Another Source.

Thanks,

Binod Suman




2 comments:

  1. HI Suman,

    Thanks for your tutorial, I am working on struts1.2 web application , i would like to call Ajax , if you have any sample program struts with ajax please share it. riyasdeen.mca@gmail.com
    once agin thanks in advance.

    ReplyDelete
  2. Nice explanation thank you..

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)