Sunday, July 4, 2010

Struts2 Data Control Include Tag Example


Struts2 Include Tag Example

Aim:
            The include tag is used to include servlet or jsp into the current page.

Procedure:

            Step 1:

            Create Project as  IncludePro

            Step 2:

            Create Action as IncludeAction.java

package example;
import com.opensymphony.xwork2.ActionSupport;

public class IncludeAction extends ActionSupport
{

    public IncludeAction()
    {
    }

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

}

Step 3:

Create jsp file

1). home.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > home JSP Page < /title >
< /head >
< body >
< s:form action="includeAction" >
< s:submit/ >
< /s:form >
< /body >
< /html >

2). success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > success JSP Page < /title >
< /head >
< body >
< s:include value="display.jsp"/ >
< /body >
< /html >

3). display.jsp



< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > success JSP Page < /title >
< /head >
< body >
SureshKumar.,M.Sc,M.Phil.,
< /body >
< /html >

Struts2 Data Control Include Tag Example


Struts2 Include Tag Example

Aim:
            The include tag is used to include servlet or jsp into the current page.

Procedure:

            Step 1:

            Create Project as  IncludePro

            Step 2:

            Create Action as IncludeAction.java

package example;
import com.opensymphony.xwork2.ActionSupport;

public class IncludeAction extends ActionSupport
{

    public IncludeAction()
    {
    }

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

}

Step 3:

Create jsp file

1). home.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > home JSP Page < /title >
< /head >
< body >
< s:form action="includeAction" >
< s:submit/ >
< /s:form >
< /body >
< /html >

2). success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > success JSP Page < /title >
< /head >
< body >
< s:include value="display.jsp"/ >
< /body >
< /html >

3). display.jsp



< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > success JSP Page < /title >
< /head >
< body >
SureshKumar.,M.Sc,M.Phil.,
< /body >
< /html >


Step 4:

Made some chages 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="default" extends="struts-default" >
< action name="includeAction" class="example.IncludeAction" >
< result name="input" < /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >

< /action >
< /package >
< /struts >


Step 5:

Run the home.jsp

Directory structure of the project

Friday, July 2, 2010

Struts2 Data control Text tag Example


Struts2 Text Tag Example


Aim:
Text tag is a generic tag that is used to get values from properties. The properties file is saved where the java file is saved.

There are three ways to use text tag.

1). To get values from *.properties file

2). If the body of the text tag is not specified, the name of the text tag will display.

3). If the body of the text tag is specified, the body of the tag will display.


Procedure:

          Step 1:

          Create Project name as DataTextPro

          Step 2:

          Create Action as HelloWorld.java

          Include following coding within the


package example;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport
{

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

Step 3:

Create jsp file

HelloWorld.jsp



< %@ page contentType="text/html; charset=UTF-8" % >

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Text tag Example < / title >

< /head >

< body >
< s:text name="body not.message" > < /s:text >


< s:text name="message" > sureshkumar, welcome< /s:text >


< /body >
< /html >

Step 4:

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


Step 5:

          Run the HelloWorld.jsp

Directory structure of the project

Thursday, July 1, 2010

Struts2 Data control Bean Tag Example

Struts2 Bean Tag Example

Aim:
Bean tag is used to instantiates the bean class and set the value and gets the value.

Procedure:

Step 1:

Create Project name as BeanPro

Step 2:

Create Action class name as BeanTag

package example;

import java.io.Serializable;

public class BeanTag implements Serializable
{
private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}

Step 3:

Create success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > JSP Page < /title >
< /head >
< body >
< s:bean name="example.BeanTag" id="uid" >
< s:param name="name" > SureshKumar < /s:param >
< s:property value="%{name}"/ >
< /s:bean >
< /body >
< /html >


Step 4:

Made some changes in struts.xml file

< !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="actionTag" class="example.actionTag" >
< result name="success" > /example/success.jsp < /result >
< /action >
< /package >
< /struts >



step 5:

Run the success.jsp

Directory structure of this project

Struts2 Data control Bean Tag Example

Struts2 Bean Tag Example

Aim:
            Bean tag is used to instantiates the bean class and set the value and gets the value.

Procedure:

            Step 1:

                        Create Project name as BeanPro

            Step 2:

                        Create Action class name as BeanTag

package example;

import java.io.Serializable;

public class BeanTag implements Serializable
{
    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

Step 3:

            Create success.jsp



< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >

< title > JSP Page < /title >
< /head >
< body >
< s:bean name="example.BeanTag" id="uid" >
< s:param name="name" > SureshKumar < /s:param >
< s:property value="%{name}"/ >
< /s:bean >
< /body >
< /html >

step 5:

            Run the success.jsp

Directory structure of this project


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

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