Showing posts with label Dispatch Action II. Show all posts
Showing posts with label Dispatch Action II. Show all posts

Sunday, June 6, 2010

Struts2-Dispatch Action Example-II


Struts2- Dispatch Action

AIM:

Dispatch action in struts2 is used to group the related function into the single action.

Problem:

            Index.jsp file contain two button one is One. Another is Two.

            If we click on One ,the one.jsp file will display.

            If we click on Two,thw\e two.jsp file will display.

Requirements:

            A). Action class(DisAction)

            B). jsp files(index.jsp,one.jsp,two.jsp)

            C). struts.xml


Procedure:

            Step 1:

                        Create Action

            Step 2:

                        Create  jsp files.(index.jsp,one.jsp,two.jsp)

            Step 3:

                        Made change in struts.xml

            Step 4:

                        Run index.jsp


Explanation:

Step 1:
            Create Action such as  DisAction

            Note:

            You follow the steps that have already posted in previous post.

            Include following coding into DisAction


package example;
import com.opensymphony.xwork2.ActionSupport;
public class DisBean extends ActionSupport
{

    public DisBean()
    {
    }

    public String execute() throws Exception
    {
        return "success";
    }
    public String one() throws Exception
    {
        return "one";
    }
    public String two() throws Exception
    {
        return "two";
    }

}

Step 2:

            Create index.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > Index < /title >
< /head >
< body >
< s:form action="User" method="post" >
< s:submit action="one" method="one" value="ONE"/ >
< s:submit action="two" method="two" value="TWO"/ >
< /s:form >
< /body >
< /html >


Create one.jsp


< html >
< head >
< title > Index < /title >
< /head >
< body >

< h1 > Hello World! < /h1 >

< b > You Are ONE < /b >

< /body >
< /html >



Create two.jsp

< html >
< head >
< title > Index < /title >
< /head >
< body >

< h1 > Hello World! < /h1 >

< b > You Are Two < /b >

< /body >
< /html >


Step 3:

Made some changes in struts.xml

< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< package name="example" namespace="/example" extends="struts-default" >
< action name="User" class="example.DisBean" >

< /action >
< action name="one" method="one" class="example.DisBean" >
< result name="one" > /example/one.jsp < /result >
< /action >
< action name="two" method="two" class="example.DisBean" >
< result name="two" > /example/two.jsp < /result >
< /action >
< /package >
< /struts >

Step 4:

Run the index.jsp


















If you click on ONE the output is

















If you click on TWO