使用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)。
更新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),我們會盡快頁面沒有值。
請加入JSF版本/ IMPL你使用 –
我已經更新了問題..使用JSF 2.1.29。 –
不知道Prettyfaces如何處理其操作。相反,我建議你在加載視圖時在facelet中的preRenderView事件中調用ViewBean#fillPage'(如果傳入的請求是回發,請檢查它,只有當它不是時) 。然後,調用'flash.keep',它應該完成你正在尋找的工作。看看[this](http://stackoverflow.com/a/25057753/1199132)來看看如何直接從EL中保留它們,但是你可以在java代碼中從託管bean執行相同的操作。 –