use*_*804 9 jsf primefaces uirepeat graphicimage
我试图显示一个面板,用户可以看到项目列表类别(显示为图像),点击后可以查看该类别中的产品(图像将显示)
为了显示项目类别,我使用了ui:重复nad支持bean calss下面是我的xhtml代码
<ui:repeat id="repeat" value="#{getData.images}" var="img" varStatus="loop">
<h:panelGroup>
<p:graphicImage id="img1" value="#{img}" alt="image not available" >
</p:graphicImage>
</h:panelGroup>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
和Managed Bean Code部分
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private List<StreamedContent> imageList = new ArrayList<StreamedContent>();
public List<StreamedContent> getImages(){
for (int i = 0; i < sdh.getNumOfImages(); i++) {
imageID = imageIDArray.get(i);
ImageService imgSer = new ImageService();
imgList.add(imageID);
imgSer.setData(imageID);
baos = imgSer.getImage();
try {
imageList.add(new DefaultStreamedContent(new
ByteArrayInputStream(baos.toByteArray())));
} catch (Exception ex) {
ex.printStackTrace();
}
}
imageNum = 0;
return imageList;
}
public StreamedContent getData() {
baos = imageList.get(imageNum);
//imageList.add(baos);
imageNum++;
return new DefaultStreamedContent(new ByteArrayInputStream(baos.toByteArray()));
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,如果我不取消注释'getData'中的'imageList.add(baos)',则不会显示图像.现在我真的想知道'ui:repeat'是如何工作的,因为'imageList'包含图像,如果在任何一种方法中都需要,我可以保存相同的图像.如果我在'getData'方法中指定固定数字(例如'imageList.get(0)'),则会多次显示相同的图像.好像我把'imageNum'放在没有'imageList.add(baos)'的情况下,它会抛出错误'流动态资源错误'
我厌倦了Bjorn Pollex的建议,并进行了必要的修改,但现在图像没有出现
Bal*_*usC 17
不可能以<p:graphicImage>这种方式使用.您应该迭代一组唯一的图像标识符,而不是遍历集合StreamedContent.这些独特的形象标识已经然后传递一个<f:param>到<p:graphicImage>这反过来将生成的浏览器正确的URL.
<ui:repeat value="#{data.imageIds}" var="imageId">
<p:graphicImage value="#{imageStreamer.image}">
<f:param name="id" value="#{imageId}" />
</p:graphicImage>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
您的#{data}托管bean必须只有:
private List<Long> imageIds; // +getter
Run Code Online (Sandbox Code Playgroud)
本#{imageStreamer}应该是范围的托管bean看起来基本上是这样一个单独的应用程序:
@ManagedBean
@ApplicationScoped
public class ImageStreamer {
@EJB
private ImageService service;
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Get ID value from actual request param.
String id = context.getExternalContext().getRequestParameterMap().get("id");
Image image = service.find(Long.valueOf(id));
return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18409 次 |
| 最近记录: |