Thursday, July 8, 2010

Struts2 Control Tag Subset Tag Example


Struts2 Subset Tag Example

Aim:
we are going to describe the subset tag. The subset tag is a generic tag that takes an iterator and outputs a subset of it.
           
Procedure:

Step 1:

            Create project for struts2 SubsetTagPro

Step 2:

            Create Action SubsetAction.java

            Include following coding into SubsetAction.java

package example;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class SubsetAction extends ActionSupport
{
    private List myList;
    public List getMyList()
    {
        return myList;
    }
    public void setMyList(List myList)
    {
        this.myList = myList;
    }
    public SubsetAction()
    {
    }
    public String execute() throws Exception
    {
        myList=new ArrayList();
        myList.add(new Integer(10));
        myList.add(new Integer(20));
        myList.add(new Integer(30));
        myList.add(new Integer(100));
        myList.add(new Integer(200));
        myList.add(new Integer(300));
        return "success";
    }
}

Step 3:

            Create jsp files

            home.jsp


< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts Subset Tag Example" >
< title > Subset Tag Example < /title >
< /head >
< body >
< s:form action="subsetAction" method="post" >
< s:submit value="GO"/ >
< /s:form >
< /body >
< /html >



SubsetAction.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Subset Example" >
< title > Struts2 Subset Example < /title >
< /head >
< body >
< s:subset source="myList" >
< s:iterator >
< s:property/ >
< /s:iterator >
< /s:subset >
< /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="subsetAction" class="example.SubsetAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/SubsetAction.jsp < /result >
< /action >
< /package >
< /struts >



Step 5:

Run home.jsp

Output










Directory Structure of this project


Struts2 Control Tag Generator Tag with id


Struts2 Generator Tag with id attribute Example

Aim:

Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page. The generator tag generates an iterator with "count" attribute and tag prints it out using the   tag. The separator attribute separates the val into entries of the iterator.

In this section, we are going to describe the generator tag using the id attributes. 

Procedure:

            Step 1:

            Create project GeneratorPro

            Step 2:

            Create Action GeneratorAction.java

            Include following coding into GeneratorAction.java

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

    public GeneratorAction()
    {
    }

    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 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:form action="generateAction" method="post" >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >

GeneratorTag.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:generator val="%{'www.suresh.com,www.suresh.net,www.suresh.org'}" separator="," count=”3” id=”mylist” / >
< %
Iterator i=(Iterator)pageContext.getAttribute("mylist");
while(i.hasNext())
{
out.println(i.next());
}
% >
< /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="generateAction" class="example.GeneratorAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/GeneratorTag.jsp < /result >
< /action >
< /package >
< /struts >


Step 5:

Run the home.jsp

Only three values displayed because we specify count is 3.

Directory structure of this project

Struts2 Control Tag Generator Tag with count


Struts2 Generator Tag Example

Aim:

Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page. The generator tag generates an iterator with "count" attribute and tag prints it out using the   tag. The separator attribute separates the val into entries of the iterator.

Procedure:

            Step 1:

            Create project GeneratorPro

            Step 2:

            Create Action GeneratorAction.java

Include following coding into GeneratorAction.java

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

    public GeneratorAction()
    {
    }

    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 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:form action="generateAction" method="post" >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >

GeneratorTag.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:generator val="%{'www.suresh.com,www.suresh.net,www.suresh.org'}" separator="," count=”3” >
< s:iterator >
< s:property/ >
< /s:iterator >
< /s:generator >
< /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="generateAction" class="example.GeneratorAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/GeneratorTag.jsp < /result >
< /action >
< /package >
< /struts >


Step 5:

Run the home.jsp

Only three values displayed because we specify count is 3.

Directory structure of this project

Struts2 Control Tag Generator Tag Example


Struts2 Generator Tag Example

Aim:

Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page.

Procedure:

            Step 1:

            Create project GeneratorPro

            Step 2:

            Create Action GeneratorAction.java

            Include following coding into GeneratorAction.java

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

    public GeneratorAction()
    {
    }

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

}

Step 3:

          Create jsp files

            home.jsp

Struts2 Control Tag Generator Tag Example


Struts2 Generator Tag Example

Aim:

Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page.

Procedure:

            Step 1:

            Create project GeneratorPro

            Step 2:

            Create Action GeneratorAction.java

            Include following coding into GeneratorAction.java

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

    public GeneratorAction()
    {
    }

    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 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:form action="generateAction" method="post" >
< s:submit value="OK"/ >
< /s:form >
< /body >
< /html >

GeneratorTag.jsp

< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< meta content="keyword" name="Struts2 Generator Tag Example" >
< title > Generator Tag Example < /title >
< /head >
< body >
< s:generator val="%{'www.suresh.com,www.suresh.net,www.suresh.org'}" separator="," >
< s:iterator >
< s:property/ >
< /s:iterator >
< /s:generator >
< /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="generateAction" class="example.GeneratorAction" >
< result name="input" > /example/home.jsp < /result >
< result name="success" > /example/GeneratorTag.jsp < /result >
< /action >
< /package >
< /struts >

Step 5:

Run the home.jsp

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


< %@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