jsf primefaces如何在文件夹中的datatgrid中显示graphicimage并在任何地方使用它?
我正在尝试显示webapp文件夹外部文件夹中的图像,其中p:graphicImage位于p:dataGrid中,但它不起作用。然而,我希望使用URL的图像显示在另一个网站上。 以下是我所尝试的:jsf primefaces如何在文件夹中的datatgrid中显示graphicimage并在任何地方使用它?,jsf,datagrid,primefaces,directory,graphicimage,Jsf,Datagrid,Primefaces,Directory,Graphicimage,我正在尝试显示webapp文件夹外部文件夹中的图像,其中p:graphicImage位于p:dataGrid中,但它不起作用。然而,我希望使用URL的图像显示在另一个网站上。 以下是我所尝试的: <!--works--> <p:graphicImage value="#{imageStreamer.getStreamedImage(fileManagerBean.resources.get(0))}" width="100"/>
<!--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>
您需要通过
传递图像标识符(例如,作为字符串的唯一文件名),而不是通过方法参数作为完整的文件
对象传递。图像将在第二次完全独立的请求中实际下载。此时将再次调用getter,但是依赖于p:dataGrid var
的方法参数将不再可用。相反,参数需要在图像的URL中结束,这正是
所做的
另见:
谢谢,我读了很多关于类似问题的帖子,但是我没有使用f:param,但现在我明白了^^。与此同时,我找到了另一个解决方案,我只是将图像从文件夹复制到我的webapp中的一个文件夹,但我认为这并不好。
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;
}
@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());
}
}