简体   繁体   English

延迟JSP自定义标签的输出?

[英]Delaying Output of JSP Custom Tag?

I want to delay the output of a custom tag. 我想延迟自定义标签的输出。

The reason being: I want to add a tag to the head of the document that will compile a list of styles and scripts to include in the page. 原因是:我想在文档的开头添加一个标签,该标签将编译要包含在页面中的样式和脚本的列表。 The subsequent tags in the page would add to the list of elements and the list would be printed once the body of the page has been generated. 页面中的后续标签将添加到元素列表中,并且在页面主体生成后将打印该列表。

Is this possible or is there a better way to do it. 这可能还是有更好的方法来做到这一点。 (I don't want to have to know what links are going to be added during the page compilation.) (我不想知道在页面编译期间要添加哪些链接。)

Thanks 谢谢

With JSPContext pushBody() and popBody() you can get some control over the order of output. 使用JSPContext pushBody()popBody()您可以控制输出顺序。

<%
  Writer body = new StringWriter();
  out = pageContext.pushBody(body);
  // following code will write to 'body' and not to client
%>
...
<%
  out = pageContext.popBody();
  // normal output again
%>
...
<% // write the captured output %>
<%= body %>

Even though it works it might be better to work around as it is very confusing. 即使它可行,也可能会更好,因为它非常令人困惑。

I have asked this question on a variety of sites and didn't get the answer I was looking for (although some were creative, Thanks!) so I ended up doing quite a bit of research on my own for the "best" way to do this. 我在各种网站上都问过这个问题,但没有得到我想要的答案(尽管有些创意,谢谢!),所以我最终还是自己做了大量研究,以寻求“最佳”方法。做这个。 This is what I came up with: 这是我想出的:

I created three custom tags so my html would look like the following: 我创建了三个自定义标签,因此我的html如下所示:

<html:page>
    <html:head>
        ...
    </html:head>
    <html:body>
        ...
    </html:body>
</html:page>

The page tag creates a variable in the REQUEST_SCOPE called "headercontents" in the doStartTag() method that can compile a list of output that I want to place in the head of the document. 页面标记在doStartTag()方法的REQUEST_SCOPE中创建一个称为“ headercontents”的变量,该变量可以编译我要放置在文档头部的输出列表。

The head element simply adds all of its body contents to this variable. head元素只是将其所有正文内容添加到此变量中。

The body element does nothing as of now, just has a tag file with a simple . 到目前为止,body元素什么也没做,只有一个带有simple的标记文件。

Any element in the body can now use the "headercontents" variable to post information to the head of the html page. 主体中的任何元素现在都可以使用“ headercontents”变量将信息发布到html页面的头部。 (Thus linking any stylesheets or scripts that it needs in the head rather than in the body). (因此,将所需的任何样式表或脚本链接在头部而不是正文中)。

Then finally in the page tag's doEndTag() method it prints the contents of the "headercontents" variable then prints the contents of itself (the contents of the html:body tag). 然后,最后在页面标签的doEndTag()方法中,它打印“ headercontents”变量的内容,然后打印其自身的内容(html:body标记的内容)。

The result is that the document can load and rearrange itself as needed. 结果是文档可以根据需要加载和重新排列。 This is still a rudimentary version but I'll perfect it and post the source code sometime in the future. 这仍然是基本版本,但我会完善它,并在将来的某个时间发布源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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