简体   繁体   中英

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

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.

This is AFAIK not possible. I would create an "init" tag (eg <foo:init /> , which would do the required output, and also put a control variable (eg 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 eg Liferay, you have to initialize your portlet objects using <portlet:defineObjects /> tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM