JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»


Iterating with tags


<iter:foreach id=»item» elements=»1,2,3,4″>


The selected item is <%= item %> <br>


</iter:foreach>


Executing the above JSP fragment generates the following content:


The selected item is 1 <br>


The selected item is 2 <br>


The selected item is 3 <br>


The selected item is 4 <br>


Let’s look at the code for the SimpleForeachTag’s handler (listing 10.1).

















}


protected void parseElements() throws JspException


{


List l = new LinkedList();



StringTokenizer st = new StringTokenizer(elements, «,») while(st.hasMoreTokens())    {


l.add(st.nextToken());


}


elementsList = l.iterator();


}


public int doAfterBody() throws JspException


try {


getBodyContent().writeOut(getPreviousOut()); getBodyContent().clear();


} catch(java.io.IOException ioe) {


// User probably disconnected … log(ls.getStr(Constants.IO_ERROR), ioe); throw new


JspTagException(ls.getStr(Constants.IO_ERROR));


}


if(elementsList.hasNext()) {


pageContext.setAttribute(id, elementsList.next()); return EVAL_BODY_TAG;


} return SKIP_BODY;


}


protected void clearProperties() {


id = null;


elements = null;


super.clearProperties();


}


protected void clearServiceState() {

Скачать в pdf «JSP Tag Libraries»