2014-05-08 75 views
0

我想從數據庫中獲得動態圖形圖像。 我在這裏發現了一些相關的問題,但不知何故,它不起作用。動態圖形圖像

頁:

<p:dataList styleClass="routeDatalist" value="#{searchRoutesBean.foundRoutes}" var="uiRoute"> 
    <p:outputLabel value="USERID #{uiRoute.owner.id}"/> 
    <h:graphicImage value="#{photoStreamer.streamedContent}" styleClass="userProfileImage"> 
    <f:param name="userId" value="#{uiRoute.owner.id}" /> 
    </h:graphicImage> 
<p:/dataList> 

我讓我的對象列表從後臺bean

@SessionScoped 
    @ManagedBean 
    public class SearchRoutesBean{ 
    private List<UIRoute> foundRoutes; 
     ... 
    } 

我創建了一個支持Bean應該採取userimage字節組,並創建一個流媒體內容

@ManagedBean(name = "photoStreamer") 
@ApplicationScoped 
public class PhotoStreamer implements Serializable { 

    @Autowired 
    UserService userService; 

    private StreamedContent streamedContent; 

    public StreamedContent getStreamedContent() { 
    ExternalContext externalContext = FacesContext.getCurrentInstance() 
     .getExternalContext(); 
    String userId = externalContext.getRequestParameterMap().get("userId"); 

    if (StringUtils.isNotBlank(userId)) { 

     User user; 
     try { 
     user = userService.getUserById(Long.valueOf(userId)); 
     byte[] image = user.getProfileJpegImage(); 
     if (image != null && image.length > 0) { 
      return new DefaultStreamedContent(new ByteArrayInputStream(
       image), "image/jpeg"); 
     } else { 
      BufferedImage bufferedImg = new BufferedImage(250, 350, 
       BufferedImage.TYPE_INT_RGB); 
      Graphics2D g2 = bufferedImg.createGraphics(); 
      g2.drawString("User has no ProfilImage", 50, 175); 
      ByteArrayOutputStream os = new ByteArrayOutputStream(); 

      ImageIO.write(bufferedImg, "png", os); 

      return new DefaultStreamedContent(new ByteArrayInputStream(
       os.toByteArray()), "image/png"); 
     } 
     } catch (NumberFormatException | UserServiceException | IOException e) { 
     throw new IllegalStateException(e); 
     } 

    } 
    return null; 

    } 
} 

我不知道爲什麼,但給定的參數userId始終爲空。

有人知道可能是什麼問題? BR

+1

本教程https://www.youtube.com/watch?v=imiBwk_xqaE – Makky

+0

這是excactly我在做什麼?唯一的區別是我使用的是dataList而不是dataTable。 – DanDiesel

+0

嘗試使用數據表,看看是否適用於你。 – Makky

回答

1

什麼

<p:dataList styleClass="routeDatalist" value="#{searchRoutesBean.foundRoutes}" var="uiRoute"> 
    <p:outputLabel value="USERID #{uiRoute.owner.id}"/> 
    <h:graphicImage value="#{photoStreamer.streamedContent(uiRoute.owner.id)}" styleClass="userProfileImage"/> 
<p:/dataList> 

+

public StreamedContent getStreamedContent(String userId) { 

if (StringUtils.isNotBlank(userId)) { 

     User user; 
     try { 
     ... 
    } 
+0

這是我也試過,但我發現問題ty :-) – DanDiesel