简体   繁体   English

jsf primefaces如何在文件夹的datatgrid中显示graphicimage并在各处使用它?

[英]jsf primefaces how to display graphicimage in datatgrid from folder and use it everywhere?

I'm trying to display images from a folder outside from my webapp folder with p:graphicImage inside ap:dataGrid but it does'nt work. 我正在尝试使用ap:dataGrid中的p:graphicImage从我的webapp文件夹之外的文件夹中显示图像,但是它不起作用。 However I wish to use URL's images for displaying them in another website. 但是,我希望使用URL的图像在另一个网站中显示它们。 Here what I tried : 这是我尝试的:

        <!--works-->
        <p:graphicImage value="#{imageStreamer.getStreamedImage(fileManagerBean.resources.get(0))}" width="100"/>
        <!--does'nt work-->
        <p:dataGrid id="dataGrid" var="file" value="#{fileManagerBean.resources}" >
            <p:commandLink action="#{fileManagerBean.setFicher(file)}" onclick="dialog.show();" update=":img,:url">
                <p:graphicImage value="#{imageStreamer.getStreamedImage(file)}" width="100"/><br/>
                <h:outputText value="#{file.name}" />
            </p:commandLink>
        </p:dataGrid>

List of files : 文件清单:

public List<File> getResources() {
        String path = "/opt/www/images";
        File resourceDirectory = new File(path);
        String[] extensions = {"png", "jpg", "jpeg", "gif"};
        Collection<File> files = FileUtils.listFiles(resourceDirectory, extensions, true);
//        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//        Set<String> resources = context.getResourcePaths("/images");
        List<File> resources = new ArrayList<File>();
        for (File resource : files) {
            resources.add(resource);
        }
        return resources;
    }

The image streamer : 图片流媒体:

@ManagedBean
@ApplicationScoped
public class ImageStreamer {

    public StreamedContent getStreamedImage(File file) {
        InputStream stream = null;
        String mimeType = null;
        try {
            stream = new FileInputStream(file);
            mimeType = URLConnection.guessContentTypeFromStream(stream);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ImageStreamer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ImageStreamer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new DefaultStreamedContent(stream, mimeType, file.getName());
    }
}

You need to pass the image identifier (eg the unique filename as String ) via <f:param> , not as a complete File object via method argument. 您需要通过<f:param>传递图像标识符(例如,唯一的文件名作为String ),而不是通过方法参数作为完整的File对象传递。 The image will namely be actually downloaded in a second, entirely independent, request. 该图像将在另一个完全独立的第二个请求中实际下载。 At that moment the getter will be invoked for a second time, however the method argument which is dependent on the p:dataGrid var will not be available anymore. 那时将再次调用getter,但是依赖于p:dataGrid var的method参数将不再可用。 Instead, the argument needs to end up in URL of the image, that's exactly what the <f:param> does. 相反,该参数需要以图像的URL结尾,这正是<f:param>作用。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM