http://www.7880.com/info/Article-44a0f840.html
Logic: The Iterator tab (hereinafter referred to as "the tag") is a tab that is very common in Struts, which is a loop to display the value of the value in the given container object, and its source code is of course needed to study, the following list Several research results: 1, the inside of this tag uses Collection to represent a given container, all given container objects (such as ArrayList, Map, etc.) will be converted into Collection2, which maintains a loop index 3, which is common Several properties are as follows: Name, Property, Scope, Id4, combined with the above label, give a source code to explain the source code of its work, you can see this sentence: Collection = tagutils.getInstance ) .lookup (pageContext, name, proty, scope); this code has been analyzed in the previous Struts source code analysis, the role is as follows: 1. If the value of the Property is null, then within the scope defined in Scope (ie Request, session, Application, Page Find objects named by Name (return value is an Object, then transforming into collection) 2, if the value of the Property is not null, then after you finish 1 step, she will call Org.apache.commons.beanutils.propertyUtils class GetProperty method, get target objects, transform into a Collection type, we can build an ArrayList when you encode, then put it in the session or request range, then in Logic: Iterator The label can be defined in this way: Name = object binds in session or Request can do not write (because no this arraylist package into an object) Scope can also write (not writing pageContext.FindAttribute method) In four scope, you are sequentially looking for), or write the code after the session or request is also very well understood. After Struts gets the collection, dynamically determine its further type, then call the relevant method to get the ITerator object, Struts use the Iterator object, Start looping Collection, remove each element object in the Collection, bind to the ID variable value on pageContext.
Seeing this, anxious person may ask, how can you end? She doesn't take it out of the element object, then display it? ============================================================================================================================================================================================================= ======= public int doStartTag () throws JspException {// Acquire the collection we are going to iterate overObject collection = this.collection;. if (collection == null) {collection = TagUtils.getInstance () lookup ( pageContext, name, property, scope);} if (collection == null) {JspException e = new JspException (messages.getMessage ( "iterate.collection"));. TagUtils.getInstance () saveException (pageContext, e); throw e;} // construct an itrator for this collectionif () collection.getclass (). Isarray ()) {Try {// if We're lucky, it is an array of objects // That We can Itereate over with no copyingItemrator = Arrays.asList ((Object []) collection) .iterator ();} catch (ClassCastException e) {// Rats - it is an array of primitivesint length = Array.getLength (collection); ArrayList c = new ArrayList (length ); for (int i = 0; i . ((Map) collection) .entrySet () iterator ();} else if (collection instanceof Enumeration) {iterator = IteratorUtils.asIterator ((Enumeration) collection);} else {JspException e = new JspException (messages.getMessage ( " iterate.iterator "));. TagUtils.getInstance () saveException (pageContext, e); throw e;} // Calculate the starting offsetif (offset == null) {offsetValue = 0;} else {try {offsetValue = Integer. parseInt (offset);} catch (NumberFormatException e) {Integer offsetObject = (Integer) TagUtils.getInstance () lookup (pageContext, offset, null);. if (offsetObject == null) {offsetValue = 0;} else {offsetValue = offsetObject.intValue ();}}} if (offsetValue <0) {offsetValue = 0;} // Calculate the rendering lengthif (length == null) {lengthValue = 0;} else {try {lengthValue = Integer.parseInt (length );} catch (NumberFormatException e) {Integer lengthObject = (Integer) TagUtils.getInstance () lookup (pageContext, length, null);. if (lengthObject == null) {lengthValue = 0;} else {lengthValue = lengthObject.int Value ();}}}} {lengthValue = 0;} lengthcount = 0; // skip the Leading elements up to the starting offsetfor (int i = 0; i ============================================================================================================================================================================================================= ====== Not anxious, in fact, the label is already over, because we will use the bean: Write label in us while we use the Logic: Iterator tag, as will be used as follows: ======= =============================================================================================================================================================================================================