如何在JSF中加载动态资源? [英] How to load dynamic resources in JSF?

查看:90
本文介绍了如何在JSF中加载动态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用h:outputScript标签将动态资源(生成的JavaScript,从技术上讲是JSON中的配置)作为动态资源加载.我知道我可以通过编写自己的servlet并通过普通的标记脚本来加载它来实现,因为我更喜欢"JSF方式"来加载资源.

I would like to load dynamic resource (generated JavaScript, technically configuration in JSON) as dynamic resource, using h:outputScript tag. I'm aware I can do that via writing my own servlet and load it via normal tag scripts, by I would prefer the "JSF-way" of loading resouces.

有可能吗?怎么办?

推荐答案

是的,有可能.您可以通过使用特定的资源库名称并具有自定义

Yes, it's possible. You can achieve this by using a specific resource library name and have a custom ResourceHandler intercept on it and return a custom Resource when a resource of the specific library is been requested.

例如

<h:outputScript library="dynamicJs" name="some.js" />

使用

public class DynamicJsResourceHandler extends ResourceHandlerWrapper {

    public DynamicJsResourceHandler(ResourceHandler wrapped) {
        super(wrapped);
    }

    @Override
    public Resource createResource(String resourceName, String libraryName) {
        if ("dynamicJs".equals(libraryName)) {
            return new DynamicJsResource(resourceName);
        } else {
            return super.createResource(resourceName, libraryName);
        }
    }

}

public class DynamicJsResource extends Resource {

    private String resourceName;

    public DynamicJsResource(String resourceName) {
        this.resourceName;
    }

    @Override
    public String getRequestPath() {
        // TODO: return "/context/javax.faces.resource/" + resourceName + ".xhtml?ln=dynamicJs";
    }

    @Override
    public URL getURL() {
        // TODO: return new URL("http://localhost:8080" + getRequestPath());
    }

    @Override
    public Map<String, String> getResponseHeaders() {
        // TODO: return desired HTTP response headers. 
    }

    @Override
    public InputStream getInputStream() throws IOException {
        // TODO: return InputStream by resourceName.
    }

    @Override
    public boolean userAgentNeedsUpdate(FacesContext context) {
        // TODO: return true when resource has been modified in server side.
    }

}

要使其运行,请在faces-config.xml中将其注册如下:

To get it to run, register it as follows in faces-config.xml:

<application>
    <resource-handler>com.example.DynamicJsResourceHandler</resource-handler>
</application>

如果您碰巧使用了JSF实用程序库 OmniFaces ,那么您还可以保存样板代码并从其<而是使用href ="http://omnifaces.org/docs/javadoc/current/org/omnifaces/resourcehandler/DynamicResource.html" rel ="nofollow noreferrer"> org.omnifaces.resourcehandler.DynamicResource ,因此您只需要实现getInputStream().如果您不使用OmniFaces,则始终可以使用其

If you happen to use JSF utility library OmniFaces, then you can also save the boilerplate code and extend from its org.omnifaces.resourcehandler.DynamicResource instead so you only need to implement getInputStream(). If you don't use OmniFaces, then you can always use its source code as guidance.

这篇关于如何在JSF中加载动态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆