如何测试某个文件是否作为JSF资源存在

时间:2014-01-13 14:28:42

标签: jsf resources

我有我的resourceHandler,资源是Jar ziped中的CSS文件作为资源。

    public class MyVersionResourceHandler extends ResourceHandlerWrapper {

        private ResourceHandler wrapped;

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

        @Override
        public Resource createResource(String resourceName) {
            return createResource(resourceName, null, null);
        }

        @Override
        public Resource createResource(String resourceName, String libraryName) {
            return createResource(resourceName, libraryName, null);
        }

        @Override
        public Resource createResource(String resourceName, String libraryName, String contentType) {
//////TODO:
            if (RESOURCE_NAME exists)
              resource1 = super.createResource(resourceName, libraryName, contentType);
            else {
              resource1 = super.createResource ("default-file", ...)
            }
//////
            final Resource resource = super.createResource(resourceName, libraryName, contentType);

            if (resource == null) {
                return null;
            }

            return new ResourceWrapper() {

                @Override
                public String getRequestPath() {
                    return super.getRequestPath();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getResourceName() {
                    return resource.getResourceName();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getLibraryName() {
                    return resource.getLibraryName();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getContentType() {
                    return resource.getContentType();
                }

                @Override
                public Resource getWrapped() {
                    return resource;
                }
            };
        }

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

    }

修改后的示例来自https://stackoverflow.com/a/18146091/2023524

如何检查指定库中的指定文件是否存在?我应该将什么作为默认文件返回?

1 个答案:

答案 0 :(得分:1)

如果资源不存在,super.createResource()将返回null。利用它。

resource1 = super.createResource(resourceName, libraryName, contentType);

if (resource1 == null) {
    resource1 = super.createResource ("default-file", ...);
}