【问题标题】:Can the Flash data be persisted after redirection?重定向后Flash数据可以保留吗?
【发布时间】: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 添加到视图页面。

&lt;f:event type="preRenderView" listener="#{ownerAndPetViewBean.fillPage}"/&gt;.

从 pretty-config.xml 中删除或注释

&lt;action onPostback="false"&gt;#{ownerAndPetViewBean.fillPage}&lt;/action&gt;

在 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


【解决方案1】:

prettyfaces 动作可能超出了当前 flash 状态的范围。不知道 pretty 动作的具体工作原理,但您需要在执行重定向的同一请求周期中设置 flash.keep(如果使用 pretty,则由 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,而无需检查回发。

另请参阅:

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 2015-02-05
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多