the action form class

Upload: redankaiahu

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 The Action Form Class

    1/6

    The ActionForm ClassIn this lesson you will learn about the ActionForm in detail. I will show you a goodexample of ActionForm. This example will help you understand Struts in detail. We will

    create user interface to accept the address details and then validate the details on server side. On the successful validation of data, the data will be sent to model (the action class).In the Action class we can add the business processing logic but in this case we are justforwarding it to the sucess.jsp.

    What is ActionForm?An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm .ActionForm maintains the session state for web application and the ActionForm object isautomatically populated on the server side with data entered from a form on the clientside.

    We will first create the class AddressForm which extends the ActionForm class. Here isthe code of the class:

    AddressForm.java

    package roseindia.net;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts.action.*;

    /*** @author Deepak Kumar* @Web http://www.roseindia.net* @Email [email protected]*/

    /** * Form bean for the Address Entry Screen. **/

    public class AddressForm extends ActionForm{ private String name= null ; private String address= null ; private String emailAddress= null ;

    public void setName(String name){ this .name=name; }

    public String getName(){ return this .name; }

    http://www.roseindia.net/struts/strutsActionForms.shtmlhttp://www.roseindia.net/struts/strutsActionForms.shtmlhttp://www.roseindia.net/struts/strutsActionForms.shtml
  • 8/9/2019 The Action Form Class

    2/6

  • 8/9/2019 The Action Form Class

    3/6

    return errors; }

    }

    The above class populates the Address Form data and validates it. The validate() methodis used to validate the inputs. If any or all of the fields on the form are blank, error messages are added to the ActionMapping object. Note that we are usingActionMessage class, ActionError is now deprecated and will be removedin next version.

    Now we will create the Action class which is the model part of the application. Our action class simply forwards the request the Success.jsp. Here is the code of theAddressAction class:

    AddressAction.java

    package roseindia.net;

    /*** @author Deepak Kumar* @Web http://www.roseindia.net* @Email [email protected]*/

    import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;

    public class AddressAction extends Action{ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ return mapping.findForward( "success" ); }}

    Now we have to create an entry for form bean in the struts-config.xml. Add the followinglines in the struts-config.xml file:

  • 8/9/2019 The Action Form Class

    4/6

    Add the following line in the struts-config.xml file for handling the action " /Address.do ":

    Now create Address.jsp, which is our form for entering the address details. Code for Address.jsp is as follows:

    Address.jsp

    Please Enter the Following Details

    Name

  • 8/9/2019 The Action Form Class

    5/6

  • 8/9/2019 The Action Form Class

    6/6

    In this lesson you learned how to create data entry form using struts, validate and finallysend process the business logic in the model part of the struts.