JSP 标记文件:检查嵌套
我正在使用 JSP 编写一些嵌套的自定义标签。 我知道有两种方法可以做到这一点:1)编写扩展 BodyTagSupport 的标签处理程序,2)编写标签文件。
在第一种方法中,代码很丑陋。 而且我找不到合并当前 struts 标记的方法。如果我使用 JspWriter 编写以下内容,我会在最终的 HTML 输出中得到此字符串,而不是由编译的 JSP 生成的有意义的输出。
<bean:write name="label">
所以,我转向第二种方法,我编写标签文件。 那么上面的问题就解决了。 但是,我在获取标签的父标签时遇到问题,因为我不知道父标签类。 如果我编写自己的标记处理程序,我可以执行以下操作,
ParentTag parent =
(ParentTag)findAncestorWithClass(this, ParentTag.class);
if (parent == null) {
throw new JspTagException("this tag is outside of its parent tag");
}
但由于我使用标记文件,所以我不太知道它的父类是什么。
我该如何解决这个问题?
I am writing some nested custom tags using JSP. I understand there are two ways to do this: 1) to write tag handler that extends BodyTagSupport, 2) to write a tag file.
In the 1st method, code is ugly. And I couldn't find a way to incorporate current struts tags.If I write the following using a JspWriter, I get this string in final HTML output instead of a meaningful output generated by the compiled JSP.
<bean:write name="label">
So, I turned to the 2nd method, I wrote tag files. Then the above issue is solved. However, I then have problem getting a tag's parent tag, since I don't know the parent tag class. I could do the following if I wrote my own tag handler,
ParentTag parent =
(ParentTag)findAncestorWithClass(this, ParentTag.class);
if (parent == null) {
throw new JspTagException("this tag is outside of its parent tag");
}
But since, I used tag files instead, I don't quite know what class its parent is.
How could I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,标签文件本身并不像标签处理程序那样是一个“类”(我的意思是,显然它是一个“类”,就像所有 JSP 文件一样,但是,您明白我的意思了)。
当我必须使用标签文件执行“嵌套”标签时,我所做的是将状态数据放入“众所周知的名称”下的请求中。 实际上是两个标签之间共享的“秘密”。
例如,在一个标记文件中,我可能会执行以下操作:
然后,在稍后的标记文件中:
然后,我可以随心所欲地操作该变量。
在父标签中,执行子标签的代码,因此在填充我的地图(在本例中)后,我可以在此时用它做任何我需要做的事情。
我发现的基本模式是将子标记的结果收集到类似列表的内容中,然后在子级完成所有工作后,父级将实际数据呈现到页面。
我的标签不需要将父级嵌套在其他父级中(即一个标签将与另一个标签的状态冲突),但显然,从一般意义上来说,这可能是此方法的问题。 不过对于我的目的来说仍然有效。
Well, a Tag file isn't a "class" per se like a Tag Handler (I mean, obviously it is, like all JSP files, but, you get my meaning).
When I have to do "nested" tags with tag files, what I do is I put my state data in to the request under a "well know name". Effectively a shared "secret" between the two tags.
For example in one tag file I might do:
Then, in a later tag file:
Then I manipulate that variable all I want.
In the parent tag, the executes the code of the child tags, so after my map (in this case) has been populated and I can do whatever I need to do with it at that point.
The basic pattern I find is to gather the results of the child tags in to something like a list and then the parent renders the actual data to the page after the children have done all their work.
I haven't had a need in my tags where I have parents nested within other parents (i.e. where one tag will conflict with the state of another), tho obviously that can be a problem with this method in a general sense. Still effective for my purposes though.
我太愚蠢了,忘记了我可以将上述Java代码插入到JSP中,并在页面上设置一个参数以将“父级”传递给JSP页面...
我不确定是否应该关闭这个问题然后。 因为这个答案有点愚蠢。 StackOverflow 的资深用户请指教。
I was stupid enough to have forgetten that I could insert the above Java code into a JSP, and set a parameter on the page to pass the "parent" to the JSP page...
I am not sure, if I should close this question then. Because the answer is somewhat stupid. Senior StackOverflow users please advise.
也许这个 会有帮助的。
Maybe this will be helpfull.