Hi,
I am trying to creat tag library for the menu to make designers job easier. got a little problem. here is what I have now:
<table>
<menu:MainMenu>
<TR>
<TD>
<P class=menu><B><menu:MenuItem/></B></P>
</TD>
</TR>
</menu:MainMenu>
</TABLE>
See Where MenuItem is, I want my calss to loop this code including at <TR> ..... </TR> and only change the MenuItem. I know one of the options is to search and replace the body that is taken in, but I am looking for a better alternative because this will significantly slow things down, I think. I am sure you guy have a solution better than search and replace, any ideas
My current
test classes look like this (of course this is just a test, all info will be pulled from the database:
public class MainMenu extends TagSupport {
private
String[] array = null;
public String[] getArray() {
return array;
}
public void setArray(String[] newArray) {
this.array = newArray;
}
public int doStartTag() throws JspException {
this.array = new String[6];
this.array[0] = "Menu 1";
this.array[1] = "Menu 2";
this.array[2] = "Menu 3";
this.array[3] = "Menu 4";
this.array[4] = "Menu 5";
this.array[5] = "Menu 6";
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
this.array = null;
return EVAL_PAGE;
}
}
and.................
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class MenuItem extends BodyTagSupport {
private int arrayCounter = 0;
private String[] array = null;
public int doStartTag() throws JspException {
MainMenu parentTag = (MainMenu)this.findAncestorWithClass(this, MainMenu.class);
this.array = parentTag.getArray();
return EVAL_BODY_BUFFERED;
}
public int doAfterBody() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print(this.array[this.arrayCounter]);
}
catch (Exception ioException) {
System.err.println("IO Exception thrown in Child.doAfterBody():");
System.err.println(ioException.toString());
throw new JspException(ioException);
}
int repeatOrSkip = SKIP_BODY;
if (this.arrayCounter < (this.array.length - 1)) {
repeatOrSkip = EVAL_BODY_AGAIN;
this.arrayCounter++;
}
return repeatOrSkip;
}
public int doEndTag() throws JspException {
try {
this.getBodyContent().writeOut(pageContext.getOut());
}
catch (Exception ioException) {
System.err.println("IO Exception thrown in Child.doEndTag():");
System.err.println(ioException.toString());
throw new JspException(ioException);
}
return EVAL_PAGE;
}
}