JSF 2.1.29 の使用
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);
}
}
pretty-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>
ビュービーン(ビュースコープ)では、
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 が null になり、viewBean の fillPage に出力される ID 文字列が null になります。私が必要とするのは更新時です。id が null であってはならない firstName を再取得する必要があります。 とにかく、このリンクを通過して、要求でフラッシュデータを永続化しましたか?
以下を試しましたが、成功しませんでした:
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 Biker の提案に従って、ビュー ページに preRenderView を追加しました。
<f:event type="preRenderView" listener="#{ownerAndPetViewBean.fillPage}"/>
.
pretty-config.xml から、削除またはコメント
<action onPostback="false">#{ownerAndPetViewBean.fillPage}</action>
fillPage メソッドに not postback チェックを配置し、マネージド Bean に flash.keep を追加しました。
FacesContext.getCurrentInstance().getExternalContext().getFlash().keep("ownerId");
上記のメソッドは、(preRenderview の代わりに使用された場合) pretty-faces アクション タグでは機能せず、値のないページが取得されます。