📄 iteratetag.java
字号:
protected String styleClass = "rescontent";
public String getStyleClass() {
return (this.styleClass);
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
protected String rendTr = "false";
public String getRendTr() {
return (this.rendTr);
}
public void setRendTr(String rendTr) {
this.rendTr = rendTr;
}
private void writeTrBegin() throws JspException{
if( rendTr.equals("true") ){
StringBuffer results = new StringBuffer();
results.append("<tr class=\"");
results.append( styleClass);
results.append("\">\n");
ResponseUtils.write(pageContext, results.toString());
}
}
private void writeTrEnd() throws JspException{
if( rendTr.equals("true") ){
StringBuffer results = new StringBuffer();
results.append("</tr>\n");
ResponseUtils.write(pageContext, results.toString());
}
}
//add by steven end
// --------------------------------------------------------- Public Methods
/**
* Construct an iterator for the specified collection, and begin
* looping through the body once per element.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
// Acquire the collection we are going to iterate over
logger.debug("Start iterate:minLengthCount<"+minLengthCount+">");
Object collection = this.collection;
if (collection == null) {
collection = RequestUtils.lookup(pageContext, name, property, scope);
}
// if (collection == null) {
// JspException e = new JspException(messages.getMessage("iterate.collection"));
// RequestUtils.saveException(pageContext, e);
// throw e;
// }
if( minLength!=null ){
try {
if( minLengthValue == -1)
minLengthValue = Integer.parseInt(minLength);
} catch (NumberFormatException e) {
minLengthValue = -1;
}
}
logger.debug("Start iterate:minLengthValue<"+minLengthValue+">");
minLengthCount++;
writeTrBegin();
if( collection == null ){
logger.info("collection is null in logic:iterate,SKIP_BODY");
if( minLengthValue == -1) return SKIP_BODY;
if( minLengthCount > minLengthValue ){
return (SKIP_BODY);
}
return (EVAL_BODY_TAG);
}
// 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"));
RequestUtils.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) RequestUtils.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) RequestUtils.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()));
}
writeTrBegin();
return (EVAL_BODY_TAG);
} else {
if( minLengthValue == -1) return SKIP_BODY;
else {
writeTrBegin();
return (EVAL_BODY_TAG);
}
}
}
/**
* Make the next collection element available and loop, or
* finish the iterations if there are no more elements.
*
* @exception JspException if a JSP exception has occurred
*/
public int doAfterBody() throws JspException {
writeTrEnd();
// Render the output from this iteration to the output stream
if (bodyContent != null) {
ResponseUtils.writePrevious(pageContext, bodyContent.getString());
bodyContent.clearBody();
}
minLengthCount++;
// Decide whether to iterate or quit
if ((lengthValue > 0) && (lengthCount >= lengthValue)) {
return (SKIP_BODY);
}
if( minLengthValue != -1 && iterator == null ) {
if( minLengthValue > (minLengthCount-1) ){
writeTrBegin();
return (EVAL_BODY_TAG);
}else{
return (SKIP_BODY);
}
}
if ( iterator.hasNext()) {
Object element = iterator.next();
if (element == null) {
pageContext.removeAttribute(id);
} else {
pageContext.setAttribute(id, element);
}
lengthCount++;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
writeTrBegin();
return (EVAL_BODY_TAG);
} else if( minLengthValue != -1) {
pageContext.removeAttribute(id);
if( minLengthValue > (minLengthCount-1) ){
writeTrBegin();
return (EVAL_BODY_TAG);
}else{
return (SKIP_BODY);
}
}else{
return (SKIP_BODY);
}
}
/**
* Clean up after processing this enumeration.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
// Clean up our started state
started = false;
iterator = null;
minLengthCount = 0;
// Continue processing this page
return (EVAL_PAGE);
}
/**
* Release all allocated resources.
*/
public void release() {
super.release();
iterator = null;
lengthCount = 0;
lengthValue = 0;
offsetValue = 0;
id = null;
collection = null;
length = null;
name = null;
offset = null;
property = null;
scope = null;
started = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -