📄 listtag.java
字号:
/*
* $Id: ListTag.java,v 1.4 2001/10/26 22:55:55 ro89390 Exp $
* Copyright 2000 Sun Microsystems, Inc. All rights reserved.
* Copyright 2000 Sun Microsystems, Inc. Tous droits r?erv?.
*/
package com.sure.taglibs.list;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;
import java.io.IOException;
import java.util.Iterator;
import java.util.Collection;
import com.sure.util.tracer.Debug;
/**
*
* This class allows you to retrieve a parameter from the request and output it to the page
* This should allow you to avoid expressions for extracting parameter info.
*
*/
public class ListTag extends BodyTagSupport {
private Iterator iterator;
private int startIndex = -1;
private int index;
private int size;
private String collectionId;
private String scope;
private Object currentItem = null;
public ListTag() {
super();
}
public void setCollectionId(String collectionId){
this.collectionId = collectionId;
}
public void setScope(String scope) {
this.scope = scope.toLowerCase();
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public void setSize(int size) {
this.size = size;
}
public int doStartTag() throws JspTagException {
Object targetObject = null;
Collection collection = null;
if (scope.equals("request")) {
targetObject = pageContext.getRequest().getAttribute(collectionId);
} else if (scope.equals("session")) {
targetObject = pageContext.getSession().getAttribute(collectionId);
} else if (scope.equals("page")) {
targetObject = pageContext.getAttribute(collectionId);
}
if (targetObject == null) {
throw new JspTagException("ListTag: Collection " + collectionId + " not found in " + scope + " scope.");
} else if (targetObject instanceof java.util.Collection) {
collection = (Collection)targetObject;
} else {
throw new JspTagException("ListTag: Iterator " + collectionId + " is not an instance of java.util.Collection.");
}
iterator = collection.iterator();
if (!iterator.hasNext()) return(SKIP_BODY);
currentItem = iterator.next();
return(EVAL_BODY_BUFFERED);
}
// process the body again until the specified length with the next item if it exists
public int doAfterBody() {
if (iterator.hasNext()) {
currentItem = iterator.next();
return(EVAL_BODY_BUFFERED);
} else return(SKIP_BODY);
}
/**
*
* Meant for use by ListItem tags
*
*/
public Object getCurrentItem() {
return currentItem;
}
public int doEndTag() throws JspTagException {
try {
BodyContent body = getBodyContent();
if (body != null) {
JspWriter out = body.getEnclosingWriter();
out.print(body.getString());
}
} catch(IOException ioe) {
System.err.println("Error handling items tag: " + ioe);
}
return EVAL_PAGE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -