Struts Application-Action-Dispatch Action
AIM:
Dispatch Action is a built in action.
Dispatch Action enables us to perform the related several action in single action class.
Example:
We have several buttons and that button will perform separate function or task.
We have a jsp page that page many button or many links such as one and two and three. If we select one, the one.jsp page will open. If we select two, the two.jsp page will open. If we select three, the three.jsp page will open.
Problem:
Dispatch.jsp page have three link. One is one and second is second and three is three.
Algorithm:
Step_1:
Creating actionforms.
Step_2:
Creating actions
Step_3:
Configure Struts-config.xml
Step_4:
Creating jsp file
Procedure:
Step_1:
Creating actionform(I have used actionforms name is disForm)
Right Click on actionforms folder.
New Other. The New File window will open.
NOTE:
Do You forget how to create actionform?. Please see Struts
Application First Struts Application.
Under categories
Select Struts
Under file types
Select Struts ActionForm Bean
Next.
New Struts ActionForm Bean windowl will open. Where you give class name (I have used actionform as disForm)
Finish.
Now the disForm class Successfully created.
We include
Private String method;
And generate setter and getter for method;
Note: I already have said how to create setter and getter .
Step_2:
Note: Do You want how to create action class with picture. Please see Struts
Applicaton-First Struts Application.
Creating action class ( I have used action class as disAction)
Right click on actions folder.
New and Other.
The New File window will open.
Where you select Struts and Struts action class.
where you give action class name and change configuration file to
org.apache.struts.actions.DispatchAction.
Next and Finish.
Now disAction class created successfully.
After Removing coding that occur between comment line.
You get like below.
package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class disAction extends DispatchAction
{
public ActionForward myAction1(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
return mapping.findForward(SUCCESS);
}
public ActionForward myAction2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
return mapping.findForward(SUCCESS);
}
}
In this coding you made change for your desire. I made change in myAction1 to one and myAction2 to two. And include code for three.
Step_3:
Configure Struts-config.xml
Open Struts-config.xml
where you give parameter attribute as method this method is created in action form.
Create Home.jsp file
< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head > < title > JSP Page < /title >
< /head >
< body >
< html:form action="/look.do?method=one" >
< html:submit value="one"/ >
< /html:form >
< html:form action="/look.do?method=two" >
< html:submit value="two"/ >
< /html:form >
< html:form action="/look.do?method=three" >
< html:submit value="three"/ >
< /html:form >
< /body >
< /html >
Step_5:
Create one.jsp and two.jsp and three.jsp
Step_6:
Run the Home.jsp
Right click on Home.jsp and Run file or Shift+f6
Conclusion:
From this post you have learned dispatch action and forward action. This actions are build in actions in struts framework.
No comments:
Post a Comment