2015-09-30 31 views
1

使用JSF 2.1.29Flash數據可以在重定向後保留嗎?

list.xhtml

<p:commandLink id="ownerViewLinkId" value="#{petOwner.firstName} #{petOwner.lastName} " 
action="#{ownerAndPetListBean.viewPetOwnerFrmList(petOwner.id,petOwner.userId,0)}" 
     onclick="PF('progrees_db').show()" 
     oncomplete="PF('progrees_db').hide()" 
     style="color:#0080FF;"> 
</p:commandLink> 

listBean(viewscoped)

public void viewPetOwnerFrmList(String ownerIdStr, String userIdStr, 
      String petIdStr) { 
    try { 
     FacesContext.getCurrentInstance().getExternalContext().getFlash().put("ownerId",ownerIdStr); 
      FacesContext.getCurrentInstance().getExternalContext().getFlash().put("userId",userIdStr); 
      FacesContext.getCurrentInstance().getExternalContext().getFlash().put("petId",petIdStr); 

      FacesContext.getCurrentInstance().getExternalContext() 
        .redirect("/ownerandpets/view"); 

    } catch (Exception e) { 
      log.warn("FL Warning", e); 
    } 
} 

漂亮-config.xml中

<url-mapping id="ownerandpetsview"> 
     <pattern value="/ownerandpets/view" />   
     <view-id value="/WEB-INF/views/ownerAndPet/OwnerAndPetsView.xhtml" /> 
     <action onPostback="false">#{ownerAndPetViewBean.fillPage}</action> 
    </url-mapping> 

個ViewBean(viewscoped)

String petIdStr; 
String userIdStr; 
String ownerIdStr; 
String firstName; 
//include getters and setters 
public void fillPage() { 

    petIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId"); 
    userIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("userId"); 
    ownerIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("ownerId"); 

    System.out.println(ownerIdStr+" "+userIdStr+" "+petIdStr); 
    firstName = getFromDBByOwnerId(ownerIdStr); 
} 

視圖頁,

<h:outputText value="#{ownerAndPetViewBean.firstName}"/> 

的firstName被重定向之後顯示。 刷新視圖頁面(通過按f5鍵),字符串firstName變爲空,並且在viewBean的fillPage中打印的id字符串爲null。 我需要刷新,我需要重新獲取ID不能爲空的名字。 已經通過這link 無論如何要堅持要求閃存數據?

試過以下,但沒有成功:

1.

FacesContext.getCurrentInstance().getExternalContext() 
        .redirect("/ownerandpets/view?faces-redirect=true"); 

2.

FacesContext.getCurrentInstance() 
    .getExternalContext().getFlash().keep("ownerId"); 
FacesContext.getCurrentInstance() 
    .getExternalContext().getFlash().keep("userId"); 
FacesContext.getCurrentInstance() 
    .getExternalContext().getFlash().keep("petId"); 

3.

FacesContext.getCurrentInstance().getExternalContext() 
    .getFlash().setKeepMessages(true); 
FacesContext.getCurrentInstance().getExternalContext() 
    .getFlash().setRedirect(true); 

在刷新調試,ID是存在於上觀看FacesContext.getCurrentInstance().getExternalContext().getFlash()閃光範圍地圖,但同樣沒有在看FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId");顯示(參見圖2)。

Values in Flash Map on Refresh - Fig 1 Fig 2

更新1(答案):

如所建議的通過的Xtreme騎車,加入preRenderView到視圖頁面。

<f:event type="preRenderView" listener="#{ownerAndPetViewBean.fillPage}"/>

從漂亮-config.xml中,刪除或評論

<action onPostback="false">#{ownerAndPetViewBean.fillPage}</action>

把一個沒有回傳在fillPage方法檢查和添加在託管bean flash.keep。

FacesContext.getCurrentInstance().getExternalContext().getFlash().keep("ownerId"); 

上述方法將不會起到很好的,面向行動標籤(如果用來代替preRenderview),我們會盡快頁面沒有值。

+0

請加入JSF版本/ IMPL你使用 –

+0

我已經更新了問題..使用JSF 2.1.29。 –

+1

不知道Prettyfaces如何處理其操作。相反,我建議你在加載視圖時在facelet中的preRenderView事件中調用ViewBean#fillPage'(如果傳入的請求是回發,請檢查它,只有當它不是時) 。然後,調用'flash.keep',它應該完成你正在尋找的工作。看看[this](http://stackoverflow.com/a/25057753/1199132)來看看如何直接從EL中保留它們,但是你可以在java代碼中從託管bean執行相同的操作。 –

回答

1

pretty faces action action action is is is is out out out out out out out scope scope scope scope scope scope scope不知道確切的動作是如何工作的,但是你需要在執行重定向的同一個請求週期中設置flash.keep(如果使用漂亮的話,這是由POST-REDIRECT-GET模式完成的)。

無論如何,由於the docs example對於漂亮的動作顯示了一個@RequestScoped的bean,因此不知道它與View Scope和flash狀態的兼容性。如果您仍在JSF 2.1中,我個人更喜歡使用f:event='preRenderView'checking for postbacks。如果JSF 2.2,使用f:viewAction,而不必檢查回傳。

參見:

相關問題