JSP标签生命周期 [英] JSP tag lifecycle

查看:96
本文介绍了JSP标签生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在代码中引入了一个错误,因为我似乎误解了jsp标签的生命周期.

I just introduced a bug into my code because I seem to have misunderstood the jsp tag lifecycle.

该标记在发生错误之前的工作方式如下: 我将标签的某些集合作为属性传递,并将其显示为表格.该集合已从控制器传递到JSP.

The tag worked like this before the bug: I pass the tag some collection as an attribute, and it displays it as a table. The collection was passed into the JSP from the controller.

错误后: 我删除了设置集合的属性.相反,在标记中,我检查集合是否为null,然后从请求中使用名称(使用命名约定)按名称进行抓取.

After the bug: I removed the attribute which set the collection. Instead, in the tag I check if the collection is null, and then grab it by name from the request (using a naming convention).

我没想到的事情: 在将集合最初设置在标记中之后,它在以后的执行中将永远不会为空!在TLD中,该属性仍被定义为非必需属性.

The thing that I didn't expect: after the collection was initially set in the tag, it would never become null on subsequent executions! It was still defined as a non-requred attribute in the TLD.

期望,该标记不会在两次执行之间保留先前的值.

I expected the tag to not hold on to previous values between executions.

推荐答案

您自己回答了该问题-已汇总.请参阅标记教程,以了解在Java实现中要实现的内容以及从此处链接的页面,其中包含调用顺序:

You answered the question yourself - it's pooled. See the tag tutorial for what to implement in java implementations, together with the page linked from there, containing the invocation sequence:

ATag t = new ATag();
t.setPageContext(...);
t.setParent(...);
t.setAttribute1(value1);
t.setAttribute2(value2);
t.doStartTag();
t.doEndTag();
t.release();

也就是说,按照API的要求在doEndTag()中重新初始化标签实例. (根据朱利安·克朗格的评论更改,谢谢)

That is, re-initialize your tag instance in doEndTag() as the API requires. (changed as of comment by Julien Kronegg, thanks)

请注意,池化可能与容器相关,但是合法(并且由于API设置,可能在任何地方都可以进行).

Note that pooling probably is container dependent, but well legal (and, due to the API setup, probably done everywhere).

这篇关于JSP标签生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆