包含来自流的JSF标记/组件的动态内容

时间:2013-11-28 14:40:10

标签: jsf-2 facelets jsf-2.2 dynamic-content

我正在开发一个应用程序,我希望从流中包含动态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标记,那么它们就会以纯文本形式打印出来。我希望它们作为输入字段呈现。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

基本上,您应该将<ui:include>与自定义ResourceHandler结合使用,以便能够以URL的风格返回资源。因此,当有OutputStream时,您应该将其写入(临时)文件,以便从中获取URL

E.g。

<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代替这个答案中提供的示例:Obtaining Facelets templates/files from an external filesystem or database。重要提示:ResourceResolver在JSF 2.2中为deprecated,支持ResourceHandler#createViewResource()

答案 1 :(得分:0)

我的JSF 2.2解决方案和自定义URLStream处理程序

public class DatabaseResourceHandlerWrapper扩展了ResourceHandlerWrapper {

private ResourceHandler wrapped;

@Inject
UserSessionBean userBeean;

public DatabaseResourceHandlerWrapper(ResourceHandler wrapped) {
    this.wrapped = wrapped;
}

@Override
public Resource createResource(String resourceName, String libraryName) {
    return super.createResource(resourceName, libraryName); //To change body of generated methods, choose Tools | Templates.
}

@Override
public ViewResource createViewResource(FacesContext context, String resourceName) {
    if (resourceName.startsWith("/dynamic.xhtml?")) {
        try {
            String query = resourceName.substring("/dynamic.xhtml?".length());
            Map<String, String> params = splitQuery(query);
            //do some query to get content
            String content = "<ui:composition"
                    + " xmlns='http://www.w3.org/1999/xhtml' xmlns:ui='http://java.sun.com/jsf/facelets'"
                    + " xmlns:h='http://java.sun.com/jsf/html'> MY CONTENT"
                    + "</ui:composition>";

            final URL url = new URL(null, "string://helloworld", new MyCustomHandler(content));
            return new ViewResource() {
                @Override
                public URL getURL() {
                    return url;
                }
            };
        } catch (IOException e) {
            throw new FacesException(e);
        }
    }

    return super.createViewResource(context, resourceName);
}

public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
    Map<String, String> params = new LinkedHashMap<>();
    String[] pairs = query.split("&");
    for (String pair : pairs) {
        int idx = pair.indexOf("=");
        params.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
    }
    return params;
}

@Override
public ResourceHandler getWrapped() {
    return wrapped;
}

static class MyCustomHandler extends URLStreamHandler {

    private String content;

    public MyCustomHandler(String content) {
        this.content = content;
    }

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        return new UserURLConnection(u, content);
    }

    private static class UserURLConnection extends URLConnection {

        private String content;

        public UserURLConnection(URL url, String content) {
            super(url);
            this.content = content;
        }

        @Override
        public void connect() throws IOException {
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(content.getBytes("UTF-8"));
        }
    }

}

}