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

IteratorGeneratorTag

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

Generate an iterator based on the val attribute supplied.

NOTE: The generated iterator will ALWAYS be pushed into the top of the stack, and poped at the end of the tag.
  • val* (Object) - the source to be parsed into an iterator
  • count (Object) - the max number (Integer, Float, Double, Long, String) entries to be in the iterator
  • separator (String) - the separator to be used in separating the val into entries of the iterator
  • id (String) - the id to store the resultant iterator into page context, if such id is supplied
  • converter (Object) - the converter (must extends off IteratorGenerator.Converter interface) to convert the String entry parsed from val into an object
Example One:
Generate a simple iterator
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
This generates an iterator and print it out using the iterator tag. Example Two:
Generate an iterator with count attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
This generates an iterator, but only 3 entries will be available in the iterator generated, namely aaa, bbb and ccc respectively because count attribute is set to 3 Example Three:
Generate an iterator with id attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," id="myAtt" />
<%
Iterator i = (Iterator) pageContext.getAttribute("myAtt");
while(i.hasNext()) {
String s = (String) i.next(); %>
<%=s%> <br/>
<% }
%>
This generates an iterator and put it in the PageContext under the key as specified by the id attribute. Example Four:
Generate an iterator with comparator attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>


public class GeneratorTagAction extends ActionSupport {

....

public Converter getMyConverter() {
return new Converter() {
public Object convert(String value) throws Exception {
return "converter-"+value;
}
};
}

...

}
This will generate an iterator with each entries decided by the converter supplied. With this converter, it simply add "converter-" to each entries.
see
org.apache.struts2.util.IteratorGenerator

Fields Summary
private static final long
serialVersionUID
public static final String
DEFAULT_SEPARATOR
private static final Log
_log
String
countAttr
String
separatorAttr
String
valueAttr
String
converterAttr
org.apache.struts2.util.IteratorGenerator
iteratorGenerator
Constructors Summary
Methods Summary
public intdoEndTag()

        // pop resulting iterator from stack at end tag
        getStack().pop();
        iteratorGenerator = null; // clean up

        return EVAL_PAGE;
    
public intdoStartTag()


        // value
        Object value = findValue(valueAttr);

        // separator
        String separator = DEFAULT_SEPARATOR;
        if (separatorAttr != null && separatorAttr.length() > 0) {
            separator = findString(separatorAttr);
        }

        // TODO: maybe this could be put into an Util class, or there is already one?
        // count
        int count = 0;
        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);
                }
            }
        }

        // converter
        Converter converter = null;
        if (converterAttr != null && converterAttr.length() > 0) {
            converter = (Converter) findValue(converterAttr);
        }


        iteratorGenerator = new IteratorGenerator();
        iteratorGenerator.setValues(value);
        iteratorGenerator.setCount(count);
        iteratorGenerator.setSeparator(separator);
        iteratorGenerator.setConverter(converter);

        iteratorGenerator.execute();



        // push resulting iterator into stack
        getStack().push(iteratorGenerator);
        if (getId() != null && getId().length() > 0) {
            // if an id is specified, we have the resulting iterator set into
            // the pageContext attribute as well
            pageContext.setAttribute(getId(), iteratorGenerator);
        }

        return EVAL_BODY_INCLUDE;
    
public voidsetConverter(java.lang.String aConverter)

        converterAttr = aConverter;
    
public voidsetCount(java.lang.String count)

        countAttr = count;
    
public voidsetId(java.lang.String string)

s.tagattribute
required="false" type="String" description="the id to store the resultant iterator into page context, if such id is supplied"

        super.setId(string);
    
public voidsetSeparator(java.lang.String separator)

s.tagattribute
required="true" type="String" description="the separator to be used in separating the val into entries of the iterator"

        separatorAttr = separator;
    
public voidsetVal(java.lang.String val)

s.tagattribute
required="true" description="the source to be parsed into an iterator"

        valueAttr = val;