【发布时间】:2015-12-28 03:25:49
【问题描述】:
使用 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>
在 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}"/>
在重定向后显示名字。 在刷新视图页面时(通过按 f5),字符串 firstName 变为空,并且在 viewBean 的 fillPage 中打印的 id 字符串为空。 我需要的是刷新,我需要重新获取 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);
在刷新调试时,观看FacesContext.getCurrentInstance().getExternalContext().getFlash()时的flash范围图中有id,但观看FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId");时不显示相同的ID(见图2)。
更新 1(答案):
按照 Xtreme Biker 的建议,将 preRenderView 添加到视图页面。
<f:event type="preRenderView" listener="#{ownerAndPetViewBean.fillPage}"/>.
从 pretty-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 以了解如何让它们直接来自 EL,但您可以在 java 代码中对托管 bean 执行相同操作。 -
是的..它在使用 preRenderView 时工作..:)
-
想发表一个冗长的评论,而不是作为答案发布;-)
标签: jsf prettyfaces flash-scope