Thursday, July 8, 2010

Struts2 Control Tag Append Tag Example


Struts2 Append Tag Example

Aim:
            Append tag is used to merge multiple iterators into one iterator.

Append Iterator Tag  is used to append iterators to form an appended iterator through which the entries goes from one iterator to another after each respective iterator is exhausted of entries.


Procedure:

            Step 1:

                        Create project AppendPro

                        I have already explained how to create project for struts2.

            Step 2:

                        Create Action AppendAction

                        I have already explained how to create action class in previous post.

                        Refer previous post to create Action.

                        Include following coding into AppendAction

package example;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.util.ArrayList;
public class AppendAction extends ActionSupport
{
    private ArrayList myList1;
    private ArrayList myList2;

    public ArrayList getMyList1() {
        return myList1;
    }

    public void setMyList1(ArrayList myList1) {
        this.myList1 = myList1;
    }

    public ArrayList getMyList2() {
        return myList2;
    }

    public void setMyList2(ArrayList myList2) {
        this.myList2 = myList2;
    }

    public AppendAction()
    {
    }
    public String execute() throws Exception
    {
        myList1=new ArrayList();
        myList1.add("suresh");
        myList1.add("kumar");
        myList1.add("praba");

        myList2=new ArrayList();
        myList2.add("chandru");
        myList2.add("sakthi");
        myList2.add("pavi");

        return "success";
    }

}

Step 3:

          Create jsp files home.jsp and success.jsp

           home.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Append Tag Example" >
< title > Home < /title >
< /head >
< body >
< s:form action="AppendAction" >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >

Step 4:

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

< s:append id="Append" >
< s:param value="myList1"/ >
< s:param value="myList2"/ >
< /s:append >
< s:iterator value="%{#Append}" >
< s:property/ >
< /s:iterator >
< /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="AppendAction" class="example.AppendAction" >
< result name="input"> /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< /action >
< /package >
< /struts >

Step 5:

Run home.jsp

Get both arraylist – output

Directory structure of this project

Struts2 Control Tag Append Tag Example


Struts2 Append Tag Example

Aim:
            Append tag is used to merge multiple iterators into one iterator.

Append Iterator Tag  is used to append iterators to form an appended iterator through which the entries goes from one iterator to another after each respective iterator is exhausted of entries.


Procedure:

            Step 1:

                        Create project AppendPro

                        I have already explained how to create project for struts2.

            Step 2:

                        Create Action AppendAction

                        I have already explained how to create action class in previous post.

                        Refer previous post to create Action.

                        Include following coding into AppendAction

package example;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.util.ArrayList;
public class AppendAction extends ActionSupport
{
    private ArrayList myList1;
    private ArrayList myList2;

    public ArrayList getMyList1() {
        return myList1;
    }

    public void setMyList1(ArrayList myList1) {
        this.myList1 = myList1;
    }

    public ArrayList getMyList2() {
        return myList2;
    }

    public void setMyList2(ArrayList myList2) {
        this.myList2 = myList2;
    }

    public AppendAction()
    {
    }
    public String execute() throws Exception
    {
        myList1=new ArrayList();
        myList1.add("suresh");
        myList1.add("kumar");
        myList1.add("praba");

        myList2=new ArrayList();
        myList2.add("chandru");
        myList2.add("sakthi");
        myList2.add("pavi");

        return "success";
    }

}

Step 3:

          Create jsp files home.jsp and success.jsp

           home.jsp

Wednesday, July 7, 2010

Struts2 Control Tag if elseif else Example


Struts2 If/ else if / else Tag Example


Aim:

            The Control Tags are used for flow control such as if/else if / else.

if, elseif and else tags only one tag evaluates at a time. Evaluation is based upon the conditions being processed. Evaluated conditions must be of Boolean type. This is illustrated in the following Jsp page.

If the condition in tag evaluates to 'true' then only this tag is evaluated and others are discarded.
If the condition in tag evaluates to 'false' and tag evaluates to 'true' then the body of the tag is processed.
If the condition in tag  and tags evaluates to 'false' then only the  tag is processed.

Procedure:

            Step 1:

                        Create Project name as ControlPro

            Step 2:

                        Create Action class ControlAction.java

                        Include following coding into ControlAction.java

                       
package example;
import com.opensymphony.xwork2.ActionSupport;
public class ControlAction extends ActionSupport
{
    private String name;

    public String getName()
    {
        return name;
    }

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

    public ControlAction()
    {
    }

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

}

Step 3:

            Create jsp files

home.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 if example" >
< title > Home jsp < /title >
< /head >
< body >
< s:form action="controlAction" >
< s:textfield name="name" label="Enter Name"/ >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >


success.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 if example" >
< title > Home jsp < /title >
< /head >
< body >

< s:if test="name=='suresh'" >
< div > If Statement < /div >
< /s:if >
< s:elseif test="name=='kumar'" >
< div > ElseIf Statement < /div >
< /s:elseif >
< s:else >
< div > Else Statement < /div >
< /s:else >

< /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="controlAction" class="example.ControlAction" >
< result name="INPUT" > /example/home.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< /action >

< /package >
< /struts >


Step 5:

Run home.jsp

How it works

You enter suresh

If Statement will display

You enter kumar

Else If statement will display

You enter anything except suresh and kumar.

Else statement will display

Directory Structure of the project

Struts2 Control Tag if else if else

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 >