2012-04-28 59 views
2

我正在嘗試構建用戶配置文件頁面以顯示有關我的用戶的一些詳細信息。顯示取決於請求參數的動態圖像

該網頁的網址類似於profile.xhtml?username=randomString

所以,我要做的是加載randomString用戶的所有數據。

由於是顯示用戶圖像的時刻,所以一切正常。

我使用primeFaces和graphicImage組件,但問題是它會導致一個NEW請求來獲取圖像,所以請求參數實際上丟失,並且getAvatar()方法接收一個空參數。

一個解決方案,可以使豆SessionScoped,但它會從第一個請求用戶獲取數據,然後它會顯示他們,即使randomString會發生變化,所以我尋求幫助:

我怎樣才能顯示來自數據庫的動態圖像取決於請求參數?

謝謝:)

編輯:新代碼如下BalusC的答覆

JSF頁面:

<c:set value="#{request.getParameter('user')}" var="requestedUser"/>      
<c:set value="#{(requestedUser==null) ? loginBean.utente : userDataBean.findUtente(request.getParameter('user'))}" var="utente"/> 
<c:set value="#{utente.equals(loginBean.utente)}" var="isMyProfile"/> 
<pou:graphicImage value="#{userDataBean.avatar}"> 
    <f:param name="username" value="#{utente.username}"/> 
</pou:graphicImage> 

(我使用這個瓦爾因爲我要顯示如果登錄用戶的個人資料頁請求只是profile.xhtml無參數)

託管豆:

@ManagedBean 
@ApplicationScoped 
public class UserDataBean { 

    @EJB 
    private UserManagerLocal userManager; 

    /** 
    * Creates a new instance of UserDataBean 
    */ 
    public UserDataBean() { 
    } 

    public Utente findUtente(String username) { 
     return userManager.getUtente(username); 
    } 

    public StreamedContent getAvatar(){ 
     String username = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("username"); 
     System.out.println(username==null); 
     Utente u = findUtente(username); 
     return new DefaultStreamedContent(new ByteArrayInputStream(u.getFoto())); 
    } 
} 

它有什麼問題? 用戶名永遠爲空!

編輯2:新增回覆BalusC

呀,因爲getAvatar()方法調用findUser(),因爲我需要找到用戶與作爲參數傳遞的用戶名實體(<f:param>不會讓我傳遞一個對象!) 。

因此findUser()會拋出異常,因爲我使用entityManager.find()null主鍵!

順便說一句,我絕對相信,由於包含圖像的面板呈現只有#{utente ne null}username是其主鍵都#{utente}#{utente.username}不爲空!

所以我不能真正檢查HTML輸出!

恐怕#{utente}是當我打電話getAvatar()爲獲取圖像需要

回答

7

通過它失去了一個新的HTTP請求爲<f:param>。它將在渲染響應期間添加。

<p:graphicImage value="#{images.image}"> 
    <f:param name="id" value="#{someBean.imageId}" /> 
</p:graphicImage> 

#{images}助手bean可以僅僅是這樣的:

@ManagedBean 
@ApplicationScoped 
public class Images { 

    @EJB 
    private ImageService service; 

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

     if (context.getRenderResponse()) { 
      // 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())); 
     } 
    } 

} 

由於上述輔助bean有沒有基於請求狀態,它可以安全地應用範圍的。

+0

感謝您的回覆! 我已經試過,但沒有成功:(我已經添加了更多的代碼,因爲我確信我做錯了什麼事情! – StepTNT 2012-04-28 13:14:53

+0

顯然'#{utente}'或'#{utente.username}'是'null'。'pou:graphicImage>'生成的HTML輸出是什麼?你正在使用哪個PrimeFaces版本? – BalusC 2012-04-28 13:41:46

+0

我在前面添加了''圖像,它顯示正確的值,因此它們都不爲空 確實,'getAvatar()'輸出請求參數爲空 我沒有html輸出,因爲'getAvatar()'調用'findUser )''拋出一個'空PK例外'我在PF 3.2 – StepTNT 2012-04-28 13:48:59