Thursday, July 8, 2010

Struts2 Control Tag Subset with start example


Struts2 Subset Tag with start 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. 
In this section, we are going to describe the subset tag using the start parameter. The start parameter is of integer type. It indicates the starting index (eg. first entry is 0) of entries in the source (needed to make available as the first entry in the resulting subset iterator).

           
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
           



No comments:

Post a Comment