📄 foreachtag.java
字号:
package examples;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.sql.*;import java.io.*;/** * Example3: A foreach tag. Will iterate over a table doing some binding * * <!-- assume a connection and a query previously defined into "query0" --> * <foreach row="row0" in="query0"> * this is some string to do * This may include <%=some expression involving row0 %> * </foreach> */public class ForEachTag extends BodyTagSupport { /** * Construct a ForEachTag */ public ForEachTag() { super(); } /** * Attributes for this action: * row * in */ /** * Attribute: row * setter and getter */ public void setRow(String s) { rowId = s; } public String getRow() { return rowId; } /** * Attribute: in * setter and getter */ public void setIn(String s) { inId = s; } public String getIn() { return inId; } /** * Process a start tag * Locate the desired result object. * * @return whether there are any rows in the result object */ public int doStartTag() throws JspTagException { result = (ResultSet) pageContext.getAttribute(inId); try { if (result.next()) { // resultset is non-empty, we just moved to next row pageContext.setAttribute(rowId, result); return EVAL_BODY_TAG; } else { return SKIP_BODY; } } catch (SQLException ex) { throw new JspTagException("error processing an SQL request ---"); } } /** * After body evaluation * * @return whether the body should be reevaluated */ public int doAfterBody() throws JspTagException { BodyContent body = getBodyContent(); try { // write evaluation on the output Writer body.writeOut(getPreviousOut()); } catch (IOException ex) { throw new JspTagException("unexpected IO error"); } // clear up so the next time we get here we are afresh. body.clearBody(); try { if (result.next()) { // resultset has more row, move over return EVAL_BODY_TAG; } else { return SKIP_BODY; } } catch (SQLException ex) { throw new JspTagException("error processing an SQL request ---"); } } /** * Process a tag end. */ public int doEndTag() { return EVAL_PAGE; } /** * Release * Clean up. */ public void release() { rowId = null; result = null; super.release(); } // variables private String rowId; // the id of the row object private String inId; // the id of the in object private ResultSet result; // the result set from the query}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -