MappingDispatchAction:


MappingDispatchAction(org.apache.struts.actions.MappingDispatchAction) is one 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 independent actions for each function.

 

MappingDispatchAction class contains multiple methods ie.. add() , edit() , search()  , save() . Here all the methods are taking the same input parameters but each method returns a different ActionForward like "add" in case of add() method , "edit" in case of edit() etc. Each ActionForward is defined in the struts-config.xml file

 



Step1: Develop a index.jsp file for ALL Operations

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:link action="add">ADD USER</html:link><BR></br>
<html:link action="update">UPDATE USER</html:link><br></br>
<html:link action="delete">DELETE USER</html:link>

Step2: Develop  a Forms and given a proper action value
Add User form
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="addExecute">   
    Name:<html:text property="sname"/><br></br>
    Password:<html:password property="spassword"/><br></br>
    Address:<html:textarea property="address"/><br></br>
    <html:submit value="REGISTER"/
</html:form>
    <br></br>
     <a href="index.jsp">HOME</a>
Update User form
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="updateExecute">
        Name:<html:text property="sname"/><br></br>
    Password:<html:password property="spassword"/><br></br>
    Address:<html:textarea property="address"/><br></br>
    <html:submit value="UPDATE"/></html:form>  <br></br>    <a href="index.jsp">HOME</a>
Delete User
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="deleteExecute">
    Name:<html:text property="sname"/><br></br>
       <html:submit value="DELETE"/>
</html:form> <br>
    <br></br>
    <a href="index.jsp">HOME</a>
 
Step4: Develop a Form Bean class and it is Configured in Struts configuration file
package com.dustbin.formbean;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class StudentFormBean extends org.apache.struts.action.ActionForm {    
    private String sname;
    private String spassword;
    private String address;
    public String getAddress() {
        return address;    }
    public void setAddress(String address) {
        this.address = address;
        System.out.println("fb...."+address);
    }
    public String getSname() {
        return sname;
        }public void setSname(String sname) {
        this.sname = sname;
        System.out.println("fb...."+sname);
    }
    public String getSpassword() {
        return spassword;
    }    public void setSpassword(String spassword) {
        this.spassword = spassword;
    }
}

Step5:Develop  a action Controller and configure in struts configuration file
package com.dustbin.controller;
import com.dustbin.formbean.StudentFormBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class StudentAction extends org.apache.struts.actions.MappingDispatchAction {
    public ActionForward addExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
                 System.out.println("calling addExecute()....");
         StudentFormBean sfb=(StudentFormBean)form;
         String name=sfb.getSname();
         String pwd=sfb.getSpassword();
         String address=sfb.getAddress();
         System.out.println("hai2....");
         request.setAttribute("name",name);
                return mapping.findForward("success");
    }
    public ActionForward updateExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         System.out.println("hai....");
         StudentFormBean sfb=(StudentFormBean)form;
         String name=sfb.getSname();
         String pwd=sfb.getSpassword();
         String address=sfb.getAddress();
         request.setAttribute("name",name);
        System.out.println("hai2....");
        return mapping.findForward("success");
    }
   public ActionForward deleteExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
          StudentFormBean sfb=(StudentFormBean)form;
          String name=sfb.getSname();
         request.setAttribute("name",name);
          return mapping.findForward("success");
    }
    }

Step6:This is for both Struts Form Bean class and Action class both are configured in struts-config.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean name="StudentFormBean" type="com.dustbin.formbean.StudentFormBean"/>
    </form-beans>
   
    <global-exceptions>
        </global-exceptions>
<global-forwards>
        <forward name="success" path="/success.jsp" redirect="false"/>
         <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>
   <action-mappings>
        <action parameter="addExecute" validate="true" name="StudentFormBean" path="/addExecute" scope="request" type="com.dustbin.controller.StudentAction"/>
        <action parameter="updateExecute" validate="true" name="StudentFormBean" path="/updateExecute" scope="request" type="com.dustbin.controller.StudentAction"/>
        <action parameter="deleteExecute" validate="true" name="StudentFormBean" path="/deleteExecute" scope="request" type="com.dustbin.controller.StudentAction"/>
       
        <action path="/add" type="org.apache.struts.actions.ForwardAction" parameter="/add.jsp"/>
        <action path="/update" type="org.apache.struts.actions.ForwardAction" parameter="/update.jsp"/>
      <action path="/delete" type="org.apache.struts.actions.ForwardAction" parameter="/delete.jsp"/>

     <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
     <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
 <message-resources parameter="com/myapp/struts/ApplicationResource"/>       
  </struts-config>

Step7:This is for Forms success page  after operation performed success.jsp page
<h3>
    Sucess..<br>
        </br>
    <%=request.getAttribute("name")%>
</h3>

Output:









Download WAR File  CLICK  HERE (WAR FILE)
DOWNLOAD RAR FILE CLICK HERE (RAR FILE)

0 comments:

Post a Comment