Here are the steps for Migrating from Struts 1 to Struts 2. You are required
to make changes in the below mentioned layers”


1.      JARs

- Add Struts 2 JARs in WEB-INF/lib


2.      web.xml

        - Add following elements to web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class> org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>

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

    <listener>
          <listener-class>
             org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

        
</web-app>

3.      struts-config.xml

        - Rename “struts-config.xml” to “struts.xml”. Technically, this file in
Struts 2 is     more streamlined.  

        A typical struts-config.xml file of Struts 1 looks like
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd ">

<struts-config>
    
    <form-beans>
  <form-bean name="MyClassForm" type="forms.MyClassForm">
  </form-bean>
    </form-beans>

  <action-mappings>
<action path="/MyClass" name="MyClassForm"    type="actions.MyClassAction" 
validate="false">
           <forward name="success" path="/Index.jsp"/>
      </action>
  </action-mappings>

  <message-resources parameter="resources"/>

</struts-config>

-       Perform the following actions:



a)      Replace DTD
b)      Replace <struts-config> tag with <struts>
c)      Include <include file="struts-default.xml"/>
d)      Remove the <form-beans> element
e)      Change <action-mappings> to <package name="hello-default"
extends="struts-default">
f)      Update each <action> element by
-       Removing from <action> element, the “name” attribute.
-       Changing the <action> “path” attribute to “name”
-       Changing the <action> “type” attribute to “class”
-       Changing the <forward> element into a <return> element

       The new struts.xml now looks like

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd ">

<struts>

    <include file="struts-default.xml"/>

    <package name="hello-default" extends="struts-default">

        <action name="MyClass" class="actions.MyClass">
            <result>/Index.jsp</result>
        </action>
    
    </package>

</struts>

4.      Action Classes

A typical ActionForm class in Struts 1 looks like

import org.apache.struts.action.ActionForm;

public class MyClassForm extends ValidatorForm {

        private String field1;

        public String getField1() {
                return field1;
        }

        public void setField1(String field1) {
                this.field1 = field1;
        }

}

A typical Action class in Struts 1 looks like

import javax.servlet.http.*;
import org.apache.struts.action.*;
public class MyClassAction extends Action {

        public ActionForward execute(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response)
                throws Exception {

                        MyClassForm input = (MyClassForm) form;
                        input.setField1(“Hello”);
                        return mapping.findForward(“success”);
        }
        
}

a) Copy all the properties from the ActionForm class to the Action class,
and remove the ActionForm class object completely. There is no ActionForm in
Struts-2.

b) Make your Action class to extend ActionSupport, instead of Action. While
this is not mandatory, it is beneficial to do so because then any POJO can
be used as an Action Object. Further we then will have access to predefined
tokens such as SUCCESS and ERROR.

c) The fileds are now a property of the only ActionSupport class. So the
values can be set directly. The way of returning “success” can be modified
to return Action.SUCCESS and return Action.ERROR

The new Action class now looks like

      
import com.opensymphony.xwork2.ActionSupport;

public class Hello extends ActionSupport {

        public String execute() throws Exception {
                setMessage(“Hello”);
                return Action.SUCCESS;
        }

        private String field1;

        public String getField1() {
                return field1;
        }
public void setField1(String field1) {
this.field1 = field1;
}


d). There is a special feature included in Struts 2 that gives the
programmer a flexibility of invoking method other than execute(). This can
be done by specifying the “method” attribute of <action> element inside file
“struts.xml”.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd ">

<struts>

    <include file="struts-default.xml"/>

    <package name="hello-default" extends="struts-default">

        <action name="MyClass" class="actions.MyClass"                  
method="myMethod">
            <result>/Index.jsp</result>
        </action>
    
    </package>

</struts>

5.      JSPs

A simple JSP in Struts 1 looks like 

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"; %>
<html>
  <head>
        <title>Hello!</title>
  </head>
  
  <form action="submit.action" method="post">   
    <body>
        Name: <input type="text" name=" field1" /><br/>
        <bean:write name="MyClassForm" property="field1" />
        <input type="submit" />
    </body>
  </form>
</html>

- Actions to be performed in this JSP are:

a)      Replace <%@ taglib %> directive
b)      Use new set of tags defined by the struts-tags.tld

The new JSP in Struts 2 would now look like

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
        <title>Hello!</title>
  </head>
  <s:form action="submit.action" method="post"> 
   <body>
        <s:textfield label="Name" name=" field1" />
        <s:property value="field1"/>
        <s:submit" />
    </body>
  </s:form>
</html>


6.      Validations

 Struts 1 developers use the Apache Commons Validator framework to validate
data. To enable validation, you must register the Validator plug-in in the
struts-config.xml file, and then make sure your own ActionForms extend
ValidatorForm instead of ActionForm. You can declare validation rules in a
validations.xml file. For example, the validation.xml file you use states
that a defect's description is required. The 'validate' method is invoked by
the Action before saving a record. When the user does not provide a
description, the Struts ActionErrors collection is populated and its
contents displayed on the view page with the help of an <html:errors/> tag.

 “validation.xml” file that validates a form, is described below:

In Struts 2, we define validation in <ActionClassName>-validation.xml file. 

For example, validations defined for MyClass-validation.xml are

<validators>
    <field name="field1">
        <field-validator type="requiredstring">
        <message key="requiredstring" />
       </field-validator>
  </field>
</validators>

Regards,
Sukrit Thareja

Nils-Helge Garli wrote:
> 
> The section about migration in the documentation [1] would be a good
> place to start.
> 
> [1] - http://struts.apache.org/2.0.11.1/docs/migration-guide.html
> 
> Nils-H
> 
> On Tue, Apr 22, 2008 at 1:17 AM, Lalchandra Rampersad
> <[EMAIL PROTECTED]> wrote:
>> How do I migrate from struts 1 to 2?
>>
>>
>>
>>  Saludos
>>
>>  Lalchandra Rampersaud
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/migration-from-struts-1-to-2-tp16819256p16916591.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to