2013-01-09 24 views

Trả lời

9

Giống như trong <p:graphicImage>, thuộc tính value có thể trỏ đến thuộc tính bean trả về StreamedContent. Điều này chỉ yêu cầu một phương thức getter đặc biệt vì những lý do được giải thích chi tiết trong câu trả lời sau về việc sử dụng <p:graphicImage> với một tài nguyên động từ một cơ sở dữ liệu: Display dynamic image from database with p:graphicImage and StreamedContent.

Trong ví dụ cụ thể của bạn, nó sẽ trông như thế này:

<p:media value="#{mediaManager.stream}" width="100%" height="300px" player="pdf"> 
    <f:param name="id" value="#{bean.mediaId}" /> 
</p:media> 

Với

@ManagedBean 
@ApplicationScoped 
public class MediaManager { 

    @EJB 
    private MediaService service; 

    public StreamedContent getStream() throws IOException { 
     FacesContext context = FacesContext.getCurrentInstance(); 

     if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { 
      // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. 
      return new DefaultStreamedContent(); 
     } else { 
      // So, browser is requesting the media. Return a real StreamedContent with the media bytes. 
      String id = context.getExternalContext().getRequestParameterMap().get("id"); 
      Media media = service.find(Long.valueOf(id)); 
      return new DefaultStreamedContent(new ByteArrayInputStream(media.getBytes())); 
     } 
    } 

} 
+2

gì nếu tôi giữ ManagedBean tôi trong @ViewScoped? –

Các vấn đề liên quan