Wednesday, June 30, 2010

Struts2 Data control Action Tag Example


Struts2 Action Tag Example

Aim:
            Action tag is used to call action directly from jsp in struts2 framework.

Note:
            I have already explained how to create struts2 project and folders for storing jsp file and for stroing action class file. I also explained where we configure the project.

Procedure:
           
Step 1:

            Create Project name as ActionTag

Step 2:

            Create Action class HelloWorld

package example;

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport
{
    String greetings = null;
    public String execute() throws Exception
    {
        setGreetings("Hello World");
        return SUCCESS;
    }
    public String getGreetings()
    {
        return greetings;
    }
    public void setGreetings(String greetings)
    {
        this.greetings = greetings;
    }
}

                       
Step 3:

            Create index.jsp file

< %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"% >
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" http://www.w3.org/TR/html4/loose.dtd >
< %@ taglib prefix="s" uri="/struts-tags" % >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
< meta content="keyword" name="struts2 action tag example" >
< title > Struts 2 Example < /title >
< /head >
< body >
< s:action name="HelloWorldAction" executeResult="true" > < /s:action >

< /body >
< /html >


Step 4:

Create HelloWorld.jsp

< %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"% >
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" http://www.w3.org/TR/html4/loose.dtd >
< %@ taglib prefix="s" uri="/struts-tags" % >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
< meta content="keyword" name="struts2 action tag example" >
< title > Struts 2 Example < /title >
< /head >
< body >


< s:property value="greetings"/ >

< /body >
< /html >


Step 5:

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="HelloWorldAction" class="example.HelloWorld" >
< result > /example/HelloWorld.jsp < /result >
< /action >
< /package >
< /struts >



Step 6:

Run the index.jsp

No comments:

Post a Comment