📄 varhellotag.java
字号:
/*
* Created on 2004-5-7
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package tag;
import java.util.ArrayList;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* @author 郝玉龙
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class VarHelloTag extends BodyTagSupport {
/** List of names passed in */
private ArrayList names;
private ArrayList positions;
/** Where we're up to in iterating over the body content */
private int index;
/** Output we're building up while iterating over the body content */
private StringBuffer output = new StringBuffer();
/**
* Getter for the names property/attribute
*/
public ArrayList getNames() {
return names;
}
/**
* Setter for the names property/attribute
*/
public void setNames(ArrayList names) {
this.names = names;
}
public int doStartTag() throws JspTagException {
//控制循环次数
if(names.size()>0){
setLoopVariables();
return EVAL_BODY_TAG;
}
return SKIP_BODY;
}
/**
* The JSP engine will call this method each time the body
* content of this tag has been processed. If it returns
* SKIP_BODY, the body content will have been processed for the
* last time. If it returns EVAL_BODY_TAG, the body will be processed
* and this method called at least once more.
* <p/>We store content in a StringBuffer, rather than write output directly.
*/
public int doAfterBody() throws JspTagException {
BodyContent bodyContent = getBodyContent();
if (bodyContent != null) {
output.append(bodyContent.getString());
try {
bodyContent.clear();
}
catch (IOException ex) {
throw new JspTagException("Fatal IO error");
}
}
// If we still haven't got to the end of the list,
// continue processing
if (++index < names.size()) {
setLoopVariables();
return EVAL_BODY_TAG;
}
// If we get to here, we've finished processing the list
return SKIP_BODY;
}
/**
* Called after processing of body content is complete.
* We use it to output the content we built up during processing
* of the body content.
*/
public int doEndTag() throws JspTagException {
BodyContent bodyContent = getBodyContent();
try {
bodyContent.getEnclosingWriter().write(output.toString());
}
catch (IOException ex) {
throw new JspTagException("Fatal IO error");
}
// We've finished processing.
// Set variables for the rest of the page
// Process the rest of the page
return EVAL_PAGE;
}
/**
* Make variable available for each iteration
*/
private void setLoopVariables() {
pageContext.setAttribute("name", names.get(index).toString());
pageContext.setAttribute("position", positions.get(index).toString());
pageContext.setAttribute("index", new Integer(index));
}
/**
* Method from NameContext interface required to provide context to
* nested tags
*/
public String getName() {
return names.get(index).toString();
}
/**
* @return Returns the positions.
*/
public ArrayList getPositions() {
return positions;
}
/**
* @param positions The positions to set.
*/
public void setPositions(ArrayList positions) {
this.positions = positions;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -