Logic: iterator label (hereinafter referred to as "this label") is a very common label in struts. Its function is to display the value in a given container object cyclically.
The source code of such a commonly used tag needs to be studied. The following lists several research results:
1. The tag uses collection internally to represent the given container. All the given container objects (such as arraylist and map) will be converted into collection objects.
2. This tag maintains its own cyclic Index
3. Several common attributes of the tag are as follows:
Name, property, scope, ID
4. Combined with the above labels, a piece of source code is provided to explain its working mechanism.
In the source code section, you can see the following sentence at the beginning:
Collection = tagutils. getinstance (). Lookup (pagecontext, name, property, scope );
The Code has been analyzed in the previous struts source code analysis, and its functions are as follows:
1. If the property value is null
Within the scope defined by scope (request, session, application, page), search for the object named after the name variable value (the returned value is an object and then converted to collection)
2. If the property value is not null
After completing step 1, she will call the getproperty method in the org. Apache. commons. beanutils. propertyutils class to get the target object and convert it to the collection type.
Therefore, when coding, we can build an arraylist by ourselves and put it in the range of session or request. Then we can define it in the logic: iterator label as follows:
Name = key value bound to the object in the session or request
Property can be left empty (because this arraylist is not encapsulated into an object)
Scope can also be left empty (if not written, the pagecontext. findattribute method call will occur and will be searched in four scopes in sequence), or write session or request
The subsequent code is also easy to understand. After struts obtains the collection, it dynamically determines its further type, and then calls the relevant method to obtain the iterator.
Finally, Struts uses the obtained iterator object to loop the collection, fetch each element object in the collection, and bind it with the ID variable value
Pagecontext. The impatient person may ask, why is it over? Does she remove the Element Object and display it?
========================================================== ========================
-
Code: select all
-
public int doStartTag() throws JspException {
// Acquire the collection we are going to iterate over
Object 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 iterator for this collection
if (collection.getClass().isArray()) {
try {
// If we're lucky, it is an array of objects
// that we can iterate over with no copying
iterator = Arrays.asList((Object[]) collection).iterator();
} catch (ClassCastException e) {
// Rats -- it is an array of primitives
int length = Array.getLength(collection);
ArrayList c = new ArrayList(length);
for (int i = 0; i < length; i++) {
c.add(Array.get(collection, i));
}
iterator = c.iterator();
}
} else if (collection instanceof Collection) {
iterator = ((Collection) collection).iterator();
} else if (collection instanceof Iterator) {
iterator = (Iterator) collection;
} else if (collection instanceof Map) {
iterator = ((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 offset
if (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 length
if (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.intValue();
}
}
}
if (lengthValue < 0) {
lengthValue = 0;
}
lengthCount = 0;
// Skip the leading elements up to the starting offset
for (int i = 0; i < offsetValue; i++) {
if (iterator.hasNext()) {
iterator.next();
}
}
// Store the first value and evaluate, or skip the body if none
if (iterator.hasNext()) {
Object element = iterator.next();
if (element == null) {
pageContext.removeAttribute(id);
} else {
pageContext.setAttribute(id, element);
}
lengthCount++;
started = true;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
return (EVAL_BODY_TAG);
} else {
return (SKIP_BODY);
}
}
========================================================== ========================
Not in a hurry. In fact, this step can be done, because while we use the logic: iterator label, we usually use the bean: Write label, as shown in the following section:
========================================================== ========================
-
Code: select all
-
<logic:iterate id="myuserinfo" name="browseresult" scope="request">
<tr>
<td align="center">
<bean:write name="myuserinfo" property="username" filter="true"/>
</td>
<td align="center">
<bean:write name="myuserinfo" property="userdesc" filter="true"/>
</td>
</tr>
</logic:iterate>
========================================================== ========================
Therefore, the bean: Write label will be included in the pagecontext, take the ID variable value as the key value, find this element object, and then retrieve and display its properties (Property Attribute definition.
OK. Now, it is clear. Finally, we need to mention the ID attribute. Many examples may misunderstand that the ID attribute definition also corresponds to an entity class (Entity Bean ),
Otherwise, we can see from the source code above that the property ID is only a key. Struts uses this value to bind each element object in the collection to pagecontext.
So the value of the ID attribute can be customized as long as one rule is followed:
The ID attribute value defined in The logic: iterator label must be consistent with the value of the name attribute in the bean: Write label below!