2012-09-06 21 views
0

簡要的用例描述:如何參數傳遞給ViewScoped豆

用戶到達頁面:http://localhost:8080/.../show.xhtml?itemId=1。 我ShowBean是@RequestScoped JSF託管Bean它通過<f:viewParam.../>獲得ID並在數據庫中查找項目:

<f:metadata> 
    <f:viewParam name="itemId" value="#{showBean.itemId}"> 
     <f:convertNumber integerOnly="#{true}"/> 
    </f:viewParam> 
</f:metadata> 
<f:event type="preRenderView" listener="#{showBean.init()}"/> 

用戶還可以編輯所顯示的項目。我想按需構建@ViewScoped ItemEditBean(通過點擊「編輯」按鈕)。要做到這一點,我做了以下內容:

<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm"> 
    <f:setPropertyActionListener target="#{itemEditBean.id}" value="#{showBean.itemId}"/> 
</p:commandButton> 

我的問題是:有沒有更好的辦法的itemId傳遞給@ViewScoped豆?

編輯:

itemEditBean.fetch(id)p:command按鈕的操作不會初始化在頁面渲染@ViewScoped豆。

<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm" action="#{itemEditBean.fetchItem(showBean.itemId)}"/>

隨着上面的代碼itemEditBean得到構建按需。

回答

0

我認爲@ViewScoped是沒有親。 嘗試使用部分表單提交。 例如像這樣,

<p:commandLink immediate="true" id="addAccessoriesLink" update=":accessoriesEntryForm"> 
     <h:graphicImage url="/images/add.png" styleClass="action-img"/> 
       <f:setPropertyActionListener value="# AccessoriesActionBean.newAccessories}" target="#{AccessoriesActionBean.newAccessories}" /> 
    </p:commandLink> 
0

問題是,showEditBeanINVOKE_APPLICATION在不保留參數後續請求重新構建。

只是確保使用f:param來保存它們。

<p:commandButton value="Edit" oncomplete="editWidget.show()" update=":itemEditForm"> 
    <f:param name="itemId" value="#{showBean.itemId}"/> 
    <f:setPropertyActionListener target="#{itemEditBean.id}" value="#{showBean.itemId}"/> 
</p:commandButton> 

P.S.你是對的@ViewScoped豆的生命週期:它是不是構建頁面加載如果只出現在action s!

+0

是的,我的對話框是dynamic =「true」,所以你的假設是錯誤的。如果你在你的'@ ViewScoped' bean中執行了任何一個返回一個空字符串的方法,'@ ViewScoped' bean就會被銷燬。 所以我都可以按需構建它,並且可以在p:對話框中使用動作來銷燬它。 –

+0

有一些綁定=「#{something}」嗎? 你怎麼知道你的bean已經被銷燬?這僅適用於。如果視圖是相同的,那麼這個bean不會被銷燬,除非用於綁定bug –

+0

不,我沒有任何綁定。如果用戶單擊對話框中的任意按鈕,我會從我的'@ ViewScoped' bean返回itemId = 1。所以基本上是的,我有