Tom said:
Maybe you already knows this, but here goes:
Yes I know what JSP is
Using dynamic JSP in such a way as you describe is a horrible idea
from a performance point of view.
The better way would be to ry refactoring your solution to use
taglibs/custom tags instead (difficult to give advice as I don't
know what problem you try to solve with generated JSP code).
Here is the problem:
I have a custom taglib I use to create menus.
For example I can do
<menu:menu>Title
<menu:item>first item</menu:item>
<menu:item>second item is a submenu
<menu:item>subitem 1</menu:item>
<menu:item>subitem 2</menu:item>
<menu:item>subitem 3</menu:item>
</menu:item>
<menu:item>third item</menu:item>
</menu:menu>
With as many levels of submenus as I want. The result uses javascript
and stuff to create a windows style dropdown menu.
Now, I have a database containing persistent objects (using Hibernate)
which have a hierarchy, i.e. each object has a set of children (in fact
it's more complicated than that, but this is enough to solve my problem
here).
So in my Java I do this:
void doMenu(Element e, StringBuffer s) {
s.append("<menu:item>");
s.append(e.getTitle());
for (Element child : e.getChildren()) {
doMenu(child, s);
}
s.append("</menu:item>");
}
then I call
s = new StringBuffer();
doMenu(rootElement, s);
and I add the <menu:menu> at begining and end.
Ok, so now what I *currently* do is write that string to a file, which
is included where needed. This is not best practice (and impossible if I
use a WAR file). I have no idea how I can do this in JSP, as there does
not seem any way of making recursive calls using a taglib.
The ideal thing would be:
<rec:defineTemplate name="mytemplate" var="e">
<menu:item>
${e.title}
<c:forEach items="${e.children}" var="child">
<rec:callTemplate name="mytemplate" value="${child}" />
</c:forEach>
</menu:item>
</rec:defineTemplate>
<menu:menu>title
<rec:callTemplate name="mytemplate" value="${rootElement}" />
</menu:menu>
Do you see what I mean?
I do not believe any such taglib exists (but I may be wrong). Plus, I
think this would completely break my taglib unless I can be sure the
<rec:> taglib is evaluated before any of the <menu:> tags...
If you can see a way of doing this, please help !
Thanks.
Regards.