[英]When declaring a taglib in jsp can I get it to output html code
Say I have in my jsp: 说我在我的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: 是否有可能获得taglib声明以输出一些html代码,所以我得到的html将是:
<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.
通常,例如,当我创建图表时,我会使用javascript来包装我特定于域的模型。 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. 但是,我创建的这些自定义标记库常常依赖于存在的JavaScript库。 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.
我希望在可能的情况下在声明taglib的地方进行此声明,因为我认为这将提供最干净的解决方案,而不是保守地始终包含该库,或者需要记住包括一个JavaScript库,这样我的标签才能工作。
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. 我将创建一个“ init”标签(例如
<foo:init />
,它将执行所需的输出,还将一个控制变量(例如fooInitialized = true
)放入页面/请求上下文中。然后,您的标签可以检查是否设置变量(这意味着使用JS初始化的输出已完成),如果没有,则抛出错误。
Similar approach uses eg Liferay, you have to initialize your portlet objects using <portlet:defineObjects />
tag. 使用类似方法,例如Liferay,您必须使用
<portlet:defineObjects />
标记初始化portlet对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.