从流中包含包含 JSF 标签/组件的动态内容 [英] Include dynamic content containing JSF tags/components from stream
问题描述
我正在开发一个应用程序,我想在其中包含来自流的动态 XHTML 内容.为了解决这个问题,我编写了一个 taghandler 扩展,它将动态 XHTML 内容转储到输出组件
I am working on an application where I would like to include dynamic XHTML content from a stream. To handle this I wrote a taghandler extension which dumps the dynamic XHTML content to output component as
UIOutput htmlChild = (UIOutput) ctx.getFacesContext().getApplication().createComponent(UIOutput.COMPONENT_TYPE);
htmlChild.setValue(new String(outputStream.toByteArray(), "utf-8"));
这适用于没有 JSF 标签的 XHTML 内容.如果我的动态 XHTML 内容中有 JSF 标记,例如 <h:inputText value="#{bean.item}"/>
,那么它们将打印为纯文本.我希望它们呈现为输入字段.我怎样才能做到这一点?
This works fine for XHTML content which has no JSF tags. If I have JSF tags in my dynamic XHTML content like <h:inputText value="#{bean.item}"/>
, then they're printed as plain text. I want them to render as input fields. How can I achieve this?
推荐答案
本质上,您应该将
与自定义 ResourceHandler
能够返回资源URL
的风格.因此,当拥有 OutputStream
时,您真的应该将其写入(临时)文件,以便您可以从中获取 URL
.
Essentially, you should be using an <ui:include>
in combination with a custom ResourceHandler
which is able to return the resource in flavor of an URL
. So when having an OutputStream
, you should really be writing it to a (temp) file so that you can get an URL
out of it.
例如
<ui:include src="/dynamic.xhtml" />
与
public class DynamicResourceHandler extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
public DynamicResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ViewResource createViewResource(FacesContext context, String resourceName) {
if (resourceName.equals("/dynamic.xhtml")) {
try {
File file = File.createTempFile("dynamic-", ".xhtml");
try (Writer writer = new FileWriter(file)) {
writer
.append("<ui:composition")
.append(" xmlns:ui='http://java.sun.com/jsf/facelets'")
.append(" xmlns:h='http://java.sun.com/jsf/html'")
.append(">")
.append("<p>Hello from a dynamic include!</p>")
.append("<p>The below should render as a real input field:</p>")
.append("<p><h:inputText /></p>")
.append("</ui:composition>");
}
final URL url = file.toURI().toURL();
return new ViewResource(){
@Override
public URL getURL() {
return url;
}
};
}
catch (IOException e) {
throw new FacesException(e);
}
}
return super.createViewResource(context, resourceName);
}
@Override
public ResourceHandler getWrapped() {
return wrapped;
}
}
(警告:基本启动示例!这会为每个请求创建一个新的临时文件,您应该自己发明一个重用/缓存系统)
在faces-config.xml
中注册如下
<application>
<resource-handler>com.example.DynamicResourceHandler</resource-handler>
</application>
<小时>
注意:以上所有内容均针对 JSF 2.2.对于偶然发现此答案的 JSF 2.0/2.1 用户,您应该使用 ResourceResolver
而不是此答案中提供的示例:从外部文件系统或数据库获取 Facelets 模板/文件.重要说明:ResourceResolver
在 JSF 2.2 中被弃用,以支持ResourceHandler#createViewResource()
.
Note: all of above is JSF 2.2 targeted. For JSF 2.0/2.1 users stumbling upon this answer, you should use ResourceResolver
instead for which an example is available in this answer: Obtaining Facelets templates/files from an external filesystem or database. Important note: ResourceResolver
is deprecated in JSF 2.2 in favor of ResourceHandler#createViewResource()
.
这篇关于从流中包含包含 JSF 标签/组件的动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!