When declaring a taglib in jsp can I get it to output html code

287 views Asked by At

Say I have in my jsp:

<%@taglib prefix="foo" uri="/WEB-INF/foo.tld"%>
<html>
....
</html>

Is it possible to get the taglib declaration to output some html code, so that my resulting html would be:

<bar>
SomeOutput
</bar>
<html>
...
</html>

I'll explain why this would be useful to me. Often I wrap my domain specific model with javascript, for example when I create a chart. Doing this gives me a reusable tag to create a chart from a model. To give more context the model might be a list of employees and the chart might be displaying their ages in a histogram or other chart.

But often these custom taglibs I create rely on a JavaScript library being present. I would like this declaration to happen where the taglib is declared if possible, as I think that this would give the cleanest solution, as opposed to conservatively having the library always included, or needing to remember to include a JavaScript library so that my tags will work.

An alternative approach would also be considered, I am curious as to what other developers do in this situation.

1

There are 1 answers

0
Jozef Chocholacek On BEST ANSWER

This is AFAIK not possible. I would create an "init" tag (e.g. <foo:init />, which would do the required output, and also put a control variable (e.g. fooInitialized = true) into page/request context. Then your tags can check, whether the variable is set (which means the output with JS initialization has been done), and throw an error, if not.

Similar approach uses e.g. Liferay, you have to initialize your portlet objects using <portlet:defineObjects /> tag.