Jsf 2 图形图像未显示在Datagrid PrimeFaces中
我在datagrid列中显示图像时遇到问题。当Datagrid加载时,它会正确显示图像,但每当我单击第二页或刷新第一页时,图像就会消失。我注意到在控制台中,单击第二页或刷新页面使参数值为空。这就是图像不显示的原因。我正在使用SessionScope。下面是我的代码:Jsf 2 图形图像未显示在Datagrid PrimeFaces中,jsf-2,primefaces,Jsf 2,Primefaces,我在datagrid列中显示图像时遇到问题。当Datagrid加载时,它会正确显示图像,但每当我单击第二页或刷新第一页时,图像就会消失。我注意到在控制台中,单击第二页或刷新页面使参数值为空。这就是图像不显示的原因。我正在使用SessionScope。下面是我的代码: public StreamedContent getStreamedImageById() { FacesContext context = FacesContext.getCurrentInstance(); if (cont
public StreamedContent getStreamedImageById() {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
System.out.println("check");
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Get ID value from actual request param.
String firstName = context.getExternalContext().getRequestParameterMap().get("firstName");
System.out.println("Name:"+firstName);
System.out.println("Image::"+images.get(firstName));
return images.get(firstName);
}
在“搜索方法”中,我只是将所有图像放入一个哈希映射中
while(itr.hasNext()){
com.sysvana.request.UserBean us=itr.next();
images.put(us.getFirstName(), stringToStreamedContent(us.getJpegPhoto()));
}
这是我的xhtml::
<p:graphicImage value="#{userManagementActionBean.streamedImageById}" height="40" width="50" style="align:center" >
<f:param id="firstName" name="firstName" value="#{user.firstName}" />
</p:graphicImage>
这个
return images.get(firstName);
这是不对的。您应该在此处创建流式内容,而不是返回以前在HTTP请求中创建的内容。关键是,您不应该在以前的HTTP请求中创建它,而应该直接在调用getStreamDimageById()
方法的同一HTTP请求中创建它
按如下方式修复它(假设userBean.getJpegPhoto()
返回byte[]
)
另见:
与具体问题无关,方法名称
stringToStreamedContent()
表明了一个主要问题。图像绝对不能表示为字符串
,而是表示为字节[]
。如果将图像视为字符串
,则可能会由于字符编码问题而丢失信息,从而导致图像损坏。您应该以byte[]
或InputStream
的形式从数据库中获取图像,谢谢您的建议。但我的问题与获取参数值有关。每当我单击datagrid分页或刷新页面时,我的意思是第二次调用getStreamDimageById()方法,param就会变为null。Ok。。现在我成功地将图像显示在网格中。谢谢你的帮助。
UserBean userBean = userService.findByFirstName(firstName);
return new DefaultStreamedContent(new ByteArrayInputStream(userBean.getJpegPhoto()));