FileDocCategorySizeDatePackage
SubsetIteratorTag.javaAPI DocExample10345Mon Jul 23 13:26:46 BST 2007org.apache.struts2.views.jsp.iterator

SubsetIteratorTag

public class SubsetIteratorTag extends org.apache.struts2.views.jsp.StrutsBodyTagSupport
NOTE: JSP-TAG

A tag that takes an iterator and outputs a subset of it. It delegates to {@link org.apache.struts2.util.SubsetIteratorFilter} internally to perform the subset functionality.

  • count (Object) - Indicate the number of entries to be in the resulting subset iterator
  • source* (Object) - Indicate the source of which the resulting subset iterator is to be derived base on
  • start (Object) - Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator
  • decider (Object) - Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator
  • id (String) - Indicate the pageContext attribute id to store the resultant subset iterator in

public class MySubsetTagAction extends ActionSupport {
public String execute() throws Exception {
l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(4));
l.add(new Integer(5));
return "done";
}


public Integer[] getMyArray() {
return a;
}

public List getMyList() {
return l;
}

public Decider getMyDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
int i = ((Integer)element).intValue();
return (((i % 2) == 0)?true:false);
}
};
}
}


<!-- s: List basic -->
<s:subset source="myList">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>


<!-- B: List with count -->
<s:subset source="myList" count="3">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>


<!-- C: List with start -->
<s:subset source="myList" count="13" start="3">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>


<!-- D: List with id -->
<s:subset id="mySubset" source="myList" count="13" start="3" />
<%
Iterator i = (Iterator) pageContext.getAttribute("mySubset");
while(i.hasNext()) {
%>
<%=i.next() %>
<% } %>


<!-- D: List with Decider -->
<s:subset source="myList" decider="myDecider">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>

s.tag
name="subset" tld-body-content="JSP" description="Takes an iterator and outputs a subset of it"

Fields Summary
private static final long
serialVersionUID
private static final Log
_log
String
countAttr
String
sourceAttr
String
startAttr
String
deciderAttr
org.apache.struts2.util.SubsetIteratorFilter
subsetIteratorFilter
Constructors Summary
Methods Summary
public intdoEndTag()


        getStack().pop();

        subsetIteratorFilter = null;

        return EVAL_PAGE;
    
public intdoStartTag()


        // source
        Object source = null;
        if (sourceAttr == null || sourceAttr.length() == 0) {
            source = findValue("top");
        } else {
            source = findValue(sourceAttr);
        }

        // count
        int count = -1;
        if (countAttr != null && countAttr.length() > 0) {
            Object countObj = findValue(countAttr);
            if (countObj instanceof Integer) {
                count = ((Integer)countObj).intValue();
            }
            else if (countObj instanceof Float) {
                count = ((Float)countObj).intValue();
            }
            else if (countObj instanceof Long) {
                count = ((Long)countObj).intValue();
            }
            else if (countObj instanceof Double) {
                count = ((Long)countObj).intValue();
            }
            else if (countObj instanceof String) {
                try {
                    count = Integer.parseInt((String)countObj);
                }
                catch(NumberFormatException e) {
                    _log.warn("unable to convert count attribute ["+countObj+"] to number, ignore count attribute", e);
                }
            }
        }

        // start
        int start = 0;
        if (startAttr != null && startAttr.length() > 0) {
            Object startObj = findValue(startAttr);
            if (startObj instanceof Integer) {
                start = ((Integer)startObj).intValue();
            }
            else if (startObj instanceof Float) {
                start = ((Float)startObj).intValue();
            }
            else if (startObj instanceof Long) {
                start = ((Long)startObj).intValue();
            }
            else if (startObj instanceof Double) {
                start = ((Long)startObj).intValue();
            }
            else if (startObj instanceof String) {
                try {
                    start = Integer.parseInt((String)startObj);
                }
                catch(NumberFormatException e) {
                    _log.warn("unable to convert count attribute ["+startObj+"] to number, ignore count attribute", e);
                }
            }
        }

        // decider
        Decider decider = null;
        if (deciderAttr != null && deciderAttr.length() > 0) {
            Object deciderObj = findValue(deciderAttr);
            if (! (deciderObj instanceof Decider)) {
                throw new JspException("decider found from stack ["+deciderObj+"] does not implement "+Decider.class);
            }
            decider = (Decider) deciderObj;
        }


        subsetIteratorFilter = new SubsetIteratorFilter();
        subsetIteratorFilter.setCount(count);
        subsetIteratorFilter.setDecider(decider);
        subsetIteratorFilter.setSource(source);
        subsetIteratorFilter.setStart(start);
        subsetIteratorFilter.execute();

        getStack().push(subsetIteratorFilter);
        if (getId() != null) {
            pageContext.setAttribute(getId(), subsetIteratorFilter);
        }

        return EVAL_BODY_INCLUDE;
    
public voidsetCount(java.lang.String count)

        countAttr = count;
    
public voidsetDecider(java.lang.String decider)

        deciderAttr = decider;
    
public voidsetSource(java.lang.String source)

        sourceAttr = source;
    
public voidsetStart(java.lang.String start)

s.tagattribute
required="false" type="Integer" description="Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator"

        startAttr = start;