struts mappingdispatchaction example

Upload: redankaiahu

Post on 30-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Struts MappingDispatchAction Example

    1/6

    Struts MappingDispatchAction Example

    Struts MappingDispatch Action (org.apache.struts.actions.MappingDispatchAction) isone of the Built-in Actions provided along with the struts framework.

    The org.apache.struts.actions.MappingDispatchAction class is a subclass of

    org.apache.struts.actions.DispatchAction class. This class enables a user to collect

    related functions into a single action class. It needs to create multiple independentactions for each function. Here in this example you will learn more about Struts

    MappingDispatchAction that will help you to grasp the concept better.

    Let's develop a class MappingDispatch_Action which is a sub class

    oforg.apache.struts.actions.MappingDispatchAction class. This class does notprovide an implementation for the execute() method because DispatchAction class itself

    implements this method.

    MappingDispatchAction class is much like the DispatchAction class except that it uses

    a unique action corresponding to a new request, to dispatch the methods . At run time,this class manages to delegate the request to one of the methods of the derived Action

    class. Selection of a method depends on the value of the parameter passed from the

    incoming request. MappingDispatchAction uses this request parameter value and selectsa corresponding action from the different action-mappings defined. This eliminates the

    need of creating an instance of ActionForm class.

    MappingDispatch_Action class contains multiple methods ie.. add() , edit() , search() ,

    save() . Here all the methods are taking the same input parameters but each method

    returns a differentActionForwardlike "add" in case ofadd() method , "edit" in case ofedit() etc. Each ActionForward is defined in the struts-config.xml file (action mapping is

    shown later in this page).

    Developing an Action Class (MappingDispatch_Action.java)

    package roseindia.net;

    /*** @author Amit Gupta* @Web http://www.roseindia.net

    * @Email [email protected]**/

    import java.io.*;import java.util.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.ServletException;import org.apache.struts.actions.MappingDispatchAction;

  • 8/9/2019 Struts MappingDispatchAction Example

    2/6

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

    public class MappingDispatch_Action extends MappingDispatchAction{

    public ActionForward add( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("You are in add function."); return mapping.findForward("add"); }

    public ActionForward edit( ActionMapping mapping, ActionForm form,

    HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("You are in edit function."); return mapping.findForward("edit"); }

    public ActionForward search( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("You are in search function"); return mapping.findForward("search");

    }public ActionForward save( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("You are in save function"); return mapping.findForward("save"); }

    }

    No need to Develop an ActionForm Class

  • 8/9/2019 Struts MappingDispatchAction Example

    3/6

    Developing the Action Mapping in the struts-config.xml

    Here, we need to create multiple independent actions for each method defined in theaction class. Note that the value specified with the parameter attributeis used to

    delegate request to the required method of the MappingDispatch_Action Class.

  • 8/9/2019 Struts MappingDispatchAction Example

    4/6

    Developing jsp page

    Code of the jsp (MappingDispatchAction.jsp) to delegate requests to different jsp pages :

    Mapping Dispatch Action Example

    Dispatch Action Example

    Call Add Section

  • 8/9/2019 Struts MappingDispatchAction Example

    5/6

    Selecting Call Add Section displays the following MappingDispatchActionAdd.jsp

    page

    Selecting Call Edit Section displays the following MappingDispatchActionEdit.jsp

    page

  • 8/9/2019 Struts MappingDispatchAction Example

    6/6

    Selecting Call Search Section displays the

    following MappingDispatchActionSearch.jsppage

    Selecting Call Save Section displays the following MappingDispatchActionSave.jsp

    page