JSF 渲染器 - 将编码输出存储在字符串中以便重新定位

发布于 2024-12-04 13:28:21 字数 753 浏览 2 评论 0原文

我想将 UIComponent 的encodeAll() 方法生成的响应存储到字符串中,以便在其他地方使用该内容。

我已经尝试设置一个 FacesContextWrapper,它提供了一个特殊的 ResponseWriter,该 ResponseWriter 应该将内容写入 StringWriter,但它不起作用。看来我的 ResponseWriter 从未被调用过。

我知道还有其他方法并在正确的位置进行编码,也许我会这样做,但我仍然想知道如何做到这一点。尤其是内容的压缩,标签之间不能有空格,不能换行,非常重要。

编辑:

我有一些方面,这些内容用于传递给javascript,因此我需要将方面的渲染内容压缩到一行。 首先,我尝试围绕 writer 获取内容,然后自己压缩它并将其作为参数附加到 javascript 中,但它不起作用。 现在我将内容渲染在正确的位置,但它没有压缩为一行。

EDIT2:

如果在行末尾使用“\n\”,Javascript 字符串只能是多行的。我无法更改encodeAll() 方法生成的内容,所以这真的很难。我真的必须捕获生成的内容,然后手动压缩字符串!但如何做到这一点呢? ResponseWriter 包装对我不起作用。

解决方案:

我想与其他人分享我的解决方案! ResponseWriter 提供了一个名为 cloneWithWriter(Writer w) 的方法,我不知道。通过将此方法与自定义 StringWriter 实现结合使用,您可以捕获缓冲区中的输出,并最终将该缓冲区用作字符串

I would like to store the response generated by the encodeAll() method of a UIComponent into a string to use the content somewhere else.

I alread tried to set up a FacesContextWrapper which provides a special ResponseWriter that should write the content to a StringWriter, but it does not work. It seems my ResponseWriter is never called.

I know there is the other way and encode in the right place and probably I will do so, but I would still like to know how to do that. Especially the compression of the content, there should be no spaces between tags and no linebreaks, is very important.

EDIT:

I have some facets and the content of these is used to be passed to javascript, because of that i would need the rendered content of the facets to be compressed to a single line.
First i tried to wrap around the writer to get the content, then compress it myself and append it to the javascript as parameter, but it didn't work.
Now i render the content at the right place, but it's not compressed to a single line.

EDIT2:

Javascript Strings can only be multilined if "\n\" is used at the end of the line. I can't change the content produced by the encodeAll() method so it's really hard. I really will have to capture the content that is generated and then manually compress the string! But how to do that? ResponseWriter-wrapping did not work for me.

SOLUTION:

I would like to share my solution with others!
ResponseWriter offers a method called cloneWithWriter(Writer w) which i didn't know. By using this method with a custom StringWriter implementation you can capture the output in a buffer and finally use that buffer as string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文