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

SortIteratorTag

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

A Tag that sorts a List using a Comparator both passed in as the tag attribute. If 'id' attribute is specified, the sorted list will be placed into the PageContext attribute using the key specified by 'id'. The sorted list will ALWAYS be pushed into the stack and poped at the end of this tag.

  • id (String) - if specified, the sorted iterator will be place with this id under page context
  • source (Object) - the source for the sort to take place (should be iteratable) else JspException will be thrown
  • comparator* (Object) - the comparator used to do sorting (should be a type of Comparator or its decendent) else JspException will be thrown


USAGE 1:
<s:sort comparator="myComparator" source="myList">
<s:iterator>
<!-- do something with each sorted elements -->
<s:property value="..." />
</s:iterator>
</s:sort>

USAGE 2:
<s:sort id="mySortedList" comparator="myComparator" source="myList" />

<%
Iterator sortedIterator = (Iterator) pageContext.getAttribute("mySortedList");
for (Iterator i = sortedIterator; i.hasNext(); ) {
// do something with each of the sorted elements
}
%>


see
org.apache.struts2.util.SortIteratorFilter
s.tag
name="sort" tld-body-content="JSP" description="Sort a List using a Comparator both passed in as the tag attribute."

Fields Summary
private static final long
serialVersionUID
String
comparatorAttr
String
sourceAttr
org.apache.struts2.util.SortIteratorFilter
sortIteratorFilter
Constructors Summary
Methods Summary
public intdoEndTag()

        int returnVal =  super.doEndTag();

        // pop sorted list from stack at the end of tag
        getStack().pop();
        sortIteratorFilter = null;

        return returnVal;
    
public intdoStartTag()

        // Source
        Object srcToSort;
        if (sourceAttr == null) {
            srcToSort = findValue("top");
        } else {
            srcToSort = findValue(sourceAttr);
        }
        if (! MakeIterator.isIterable(srcToSort)) { // see if source is Iteratable
            throw new JspException("source ["+srcToSort+"] is not iteratable");
        }

        // Comparator
        Object comparatorObj = findValue(comparatorAttr);
        if (! (comparatorObj instanceof Comparator)) {
            throw new JspException("comparator ["+comparatorObj+"] does not implements Comparator interface");
        }
        Comparator c = (Comparator) findValue(comparatorAttr);

        // SortIteratorFilter
        sortIteratorFilter = new SortIteratorFilter();
        sortIteratorFilter.setComparator(c);
        sortIteratorFilter.setSource(srcToSort);
        sortIteratorFilter.execute();

        // push sorted iterator into stack, so nexted tag have access to it
        getStack().push(sortIteratorFilter);
        if (getId() != null && getId().length() > 0) {
            pageContext.setAttribute(getId(), sortIteratorFilter);
        }

        return EVAL_BODY_INCLUDE;
    
public voidsetComparator(java.lang.String comparator)

        comparatorAttr = comparator;
    
public voidsetSource(java.lang.String source)

        sourceAttr = source;