Struts MappingDispatch Action (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. 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 of org.apache.struts.actions.MappingDispatchAction class. This class does not provide an implementation for the execute() method because DispatchAction class itself implements this method.
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 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 (action mapping is shown later in this page).
Developing an Action Class (MappingDispatch_Action.java)
package roseindia.net;
|
No need to Develop an ActionForm Class
Developing the Action Mapping in the struts-config.xml
Here, we need to create multiple independent actions for each method defined in the action class. Note that the value specified with the parameter attribute is used to delegate request to the required method of the MappingDispatch_Action Class.
parameter="add" input="/pages/MappingDispatchAction.jsp" scope="request" validate="false"> parameter="edit" input="/pages/MappingDispatchAction.jsp" scope="request" validate="false"> parameter="search" input="/pages/MappingDispatchAction.jsp" scope="request" validate="false"> parameter="save" input="/pages/MappingDispatchAction.jsp" scope="request" validate="false"> |
Developing jsp page
Code of the jsp (MappingDispatchAction.jsp) to delegate requests to different jsp pages :
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> Dispatch Action Example |
Add the following line in the index.jsp to call the form.
Example demonstrates how MappingDispatchAction class works.
Building and Testing the Example
To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the MappingDispatchAction.jsp page. Your browser displays the following MappingDispatchAction page.
Selecting Call Add Section displays the following MappingDispatchActionAdd.jsp page
Selecting Call Edit Section displays the following MappingDispatchActionEdit.jsp page
Selecting Call Search Section displays the following MappingDispatchActionSearch.jsp page
Selecting Call Save Section displays the following MappingDispatchActionSave.jsp page
0 comments:
Post a Comment