Tuesday, July 6, 2010

Struts2 Data Control Set Tag Example


Struts2 Set Tag Example

Solution:

            Example for set tag , property tag
Aim:
The set tag is a generic tag that is used to assign a value to a variable in a specified scope.

Procedure:

            Step 1:

                        Create Project PropertyPro

            Step 2:

                        Create LoginAction

                        Include following coding into LoginAction

package example;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
    private String username;
    private String password;

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public LoginAction()
    {
    }

    public String execute() throws Exception
    {
       if((getUsername().equals("suresh"))&&(getPassword().equals("kumar")))
       {
           return "success";
       }
       else
       {
           return "failure";
       }
    }

}          

Step 3:

            Create jsp files.

            home.jsp


 


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< s:form action="LoginAction" method="post" >
< s:textfield label="UserName" name="username"/ >
< s:password label="PassWord" name="password"/ >
< s:submit value="Login"/ >
< /s:form >
< /body >
< /html >

success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >

< h2 > Welcome < /h2 >

< h1 > < s:property value="username"/ > < /h1 > < h2 > You Are Welcome < /h2 >
< s:set name="username" value="'suji'"/ >
< s:property value="#username"/ >
< /body >
< /html >


failure.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< h1 > < s:property value="username"/ > < /h1 > < h3 > You Are Not Valid User. < /h3 >
< /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" extends="struts-default" >
< action name="LoginAction" class="example.LoginAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >

Step 5:
Run home.jsp

Directory Structure of this project

Struts2 Data Control Set Tag Example


Struts2 Set Tag Example

Solution:

            Example for set tag , property tag
Aim:
The set tag is a generic tag that is used to assign a value to a variable in a specified scope.

Procedure:

            Step 1:

                        Create Project PropertyPro

            Step 2:

                        Create LoginAction

                        Include following coding into LoginAction

package example;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
    private String username;
    private String password;

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public LoginAction()
    {
    }

    public String execute() throws Exception
    {
       if((getUsername().equals("suresh"))&&(getPassword().equals("kumar")))
       {
           return "success";
       }
       else
       {
           return "failure";
       }
    }

}          

Step 3:

            Create jsp files.

            home.jsp


 


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< s:form action="LoginAction" method="post" >
< s:textfield label="UserName" name="username"/ >
< s:password label="PassWord" name="password"/ >
< s:submit value="Login"/ >
< /s:form >
< /body >
< /html >

success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >

< h2 > Welcome < /h2 >

< h1 > < s:property value="username"/ > < /h1 > < h2 > You Are Welcome < /h2 >
< s:set name="username" value="'suji'"/ >
< s:property value="#username"/ >
< /body >
< /html >


failure.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Property Tag Example" >
< title > JSP Page < /title >
< /head >
< body >
< h1 > < s:property value="username"/ > < /h1 > < h3 > You Are Not Valid User. < /h3 >
< /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" extends="struts-default" >
< action name="LoginAction" class="example.LoginAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >

Step 5:
Run home.jsp

Directory Structure of this project

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