包含流中包含JSF标签/组件的动态内容 [英] Include dynamic content containing JSF tags/components from stream
问题描述
我正在开发一个应用程序,该应用程序要包含流中的动态XHTML内容.为了解决这个问题,我编写了一个taghandler扩展程序,该扩展程序将动态XHTML内容转储为
到输出组件.UIOutput htmlChild = (UIOutput) ctx.getFacesContext().getApplication().createComponent(UIOutput.COMPONENT_TYPE);
htmlChild.setValue(new String(outputStream.toByteArray(), "utf-8"));
这对于没有JSF标签的XHTML内容很好用.如果我在动态XHTML内容(例如<h:inputText value="#{bean.item}"/>
)中具有JSF标记,则它们将被打印为纯文本.我希望它们呈现为输入字段.我怎样才能做到这一点?
本质上,您应该将 使用 (警告:基本的启动示例!这会在每个请求上创建一个新的临时文件,应自行创建一个重用/缓存系统) 其在
注意:以上所有都是针对JSF 2.2的.对于JSF 2.0/2.1用户,绊脚石回答此问题,您应该使用 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 This works fine for XHTML content which has no JSF tags. If I have JSF tags in my dynamic XHTML content like Essentially, you should be using an E.g. with (warning: basic kickoff example! this creates a new temp file on every request, a reuse/cache system should be invented on your own) which is registered in
Note: all of above is JSF 2.2 targeted. For JSF 2.0/2.1 users stumbling upon this answer, you should use 这篇关于包含流中包含JSF标签/组件的动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<ui:include>
与自定义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>
ResourceResolver
,此回答中提供了一个示例:不推荐使用,而支持ResourceHandler#createViewResource()
. /p>UIOutput htmlChild = (UIOutput) ctx.getFacesContext().getApplication().createComponent(UIOutput.COMPONENT_TYPE);
htmlChild.setValue(new String(outputStream.toByteArray(), "utf-8"));
<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? <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
as follows<application>
<resource-handler>com.example.DynamicResourceHandler</resource-handler>
</application>
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()
.